|
发表于 2013-11-27 10:04:46
|
显示全部楼层
|阅读模式
来自 中国–广东–深圳
昨天试了一下通过php脚本上传文件的例子,代码如下:
<?
$file_path = "upload/";
$file_name = $_FILES["upfile"];
//上传目录,不存在就
if(!is_dir($file_path))
{
mkdir($file_path, 0666);
}
if(!empty($_FILES))
{
echo "上传文件 ".$file_name["name"]." 大小 ".$file_name["size"]."<br>";
if($file_name["size"] > 2097152)
{
exit("文件大小超过了2M."."<br>");
}
if($file_name["error"] > 0)
{
exit("文件上传发生错误:".$file_name["error"]."<br>");
}
echo "上传临时文件为:".$file_name["tmp_name"]."<br>";
$tmp_arr = explode(".", $file_name["name"]);
$file_ext = array_pop($tmp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
$new_file = time().".$file_ext";
if (is_uploaded_file($file_name["tmp_name"]))
{
echo $file_name["tmp_name"]." 为通过http post 上传文件.<br>";
}
else
{
echo("不允许上传文件.<br>");
switch($file_name["error"])
{
case 0://no error; possible file attack!
echo "there is a problem with your upload.<br>";
break;
case 1://uploaded file exceeds the upload_max_filesize directive in php.ini
echo "the file you try to upload is too big.<br>";
break;
case 2://uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
echo "the file you try to upload is too big.<br>";
break;
case 3://uploaded file was only partially uploaded
echo "the file you try to upload was only partially uploaded.<br>";
break;
case 4: //no file was uploaded
echo "you must select an image for upload.";
break;
default: //a default error, just in case! :)
echo "there was a problem with your upload.";
break;
}
exit;
}
if(move_uploaded_file($file_name["tmp_name"], $file_path.$file_name["name"]))
//if(copy($file_name["tmp_name"], $file_path.$new_file))
{
echo "移动文件".$file_name["tmp_name"]."成功.<br>";
}
else
{
echo "移动文件 ".$file_name["tmp_name"]." 失败. 错误码 : ".$file_name["error"]."<br>";
}
exit;
}
else
{
echo "无正确的文件上传.<br>";
}
?>
输出结果:
上传文件 1.jpg 大小 213734
上传临时文件为:/tmp/phpo5UTjq
/tmp/phpo5UTjq 为通过http post 上传文件.
移动文件 /tmp/phpo5UTjq 失败. 错误码 : 0
经过几次尝试后,把mkdir时目录权限修改为766就可以正常移动文件,如下:
mkdir($file_path, 0766);
可是理解起来有点怪,要往一个目录移动文件,应该只要有写权限就可以了,为什么还要执行权限呢?
|
|