龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

为Hanking解读代码

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
span style=font-family:微软雅黑, Verdana, sans-serif, 宋体;font-size:14px;line-height:22px;text-align:left;background-color:#f9f9f9;@Hanking : 想把这个php转换成jsp的,但是php一点都不懂,能不能给我讲讲?br / br
@Hanking : 想把这个php转换成jsp的,但是php一点都不懂,能不能给我讲讲?

解读一下


<?php
// 上传的最终目录
$dirname = "/tmp/phonegap/uploads";
// 如果有上传文件的话
// 凡是以上传的方式post的话,这个$_FILES都会有内容
// 这里最好改为 if (!empty($_FILES))
if ($_FILES) {
	// 打印出上传的内容
	print_r($_FILES);
	// 创建上传的最终目录, mkdir(目录名称, 目录访问权限, 如果目录不存在是否递归创建目录)
	// 这里最好判断一下if (!is_dir($dirname))
	mkdir ($dirname, 0777, true);
	// 将上传文件从临时目录中转移到最终的目录。
	// 这里说一下,php的上传模式,所有php的上传,是默认传到系统的临时文件夹中,
	// 如果你不调用move_uploaded_file,他是不会将文件移动到你想要目录去的。
	// 如果你制定了php.ini中,上传的临时文件,临时文件夹则是php.ini中指定的文件夹。
	move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
}
// 如果uri中的query string中,有image=xxxxx的方式的话
else if (isset($_GET['image'])) {
	// 生成该图片保存到本地的全路径
	// 这里不做过滤和检查,很危险的哦
	$file = $dirname."/".$_GET['image'];
	// 指定当前请求转为jpeg的header输出
	header('Content-type: image/jpeg');
	// 取得图片的信息
	list($width, $height) = getimagesize($file);
	// 这里是重新调整图片的大小
	$newWidth = 120.0;
	$size = $newWidth / $width;
	$newHeight = $height * $size;
	$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
	// 将图片生成到$file的路径中去
	$image = imagecreatefromjpeg($file);
	imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
	// 并且创建一张品质为80的缩略图预览
	imagejpeg($resizedImage, null, 80);
}
// If displaying images
else {
	// 这个是生成当前服务器的基础url
	$baseURI = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
	// 获取上传目录下的文件名
	$images = scandir($dirname);
	$ignore = Array(".", "..");
	// 如果存在文件的话,
	// 这里最好改为 if (!empty($images))
	if ($images) {
		// 遍历这个目录下的文件,并且生成<img>以供观看
		// 不过这写法也太不讲究了,不分目录和文件
		foreach($images as $curimg){
			if (!in_array($curimg, $ignore)) {
				echo "Image: ".$curimg."<br>";
				echo "<img src='".$baseURI."?image=".$curimg."&rnd=".uniqid()."'><br>";
			}
		}
	}
	else {
		echo "No images on server";
	}
}
?>
精彩图集

赞助商链接