[英]move_uploaded_file() function doesn't work while referencing to another page
我将move_upload_file()函数引用到另一页时遇到问题。 它可以正常工作。 但是问题是所选文件没有被传输到指定目录。
我不知道这是什么问题,不胜感激任何帮助或建议。
这是代码:
第1页(正在完成输入):
<form name='NewsForm' method='post' action='conf_news.php' enctype='multipart/form-data' >
<input type="file" name="file" id="file" value='' >
<input type='submit' name='AddNews' value='POST' >
</form>
它将代码带到另一个页面“ conf_news.php”,在该页面中,我引用了部分存储文件:PAGE 2:
$PicMessage = "";
$PicName = addslashes( $_FILES["file"]["name"] );
$PicType = addslashes( $_FILES['file']['type'] );
$PicSize = addslashes( $_FILES['file']['size'] );
$PicError = addslashes( $_FILES["file"]["error"] );
$PicTempName = addslashes( $_FILES["file"]["tmp_name"] );
$allowedExt = array('jpeg', 'jpg', 'gif', 'png');
$a = explode(".", $PicName);
$extension = end($a); unset($a);
if((($PicType == "image/gif")
|| ($PicType == "image/png")
|| ($PicType == "image/jpeg")
|| ($PicType == "image/jpg")
&& ($PicSize > 100)
&& in_array($extentions, $allowedExt))){
if($PicError > 0){
$PicMessage = "Error Type: ". $PicError. "<br />";
}
else{
echo "Upload: ". $PicName. "<br />";
echo "Type: ". $PicType. "<br />";
echo "Size: ". ($PicSize / 1024). " Kb<br />";
echo "Stored in: ". $PicTempName;
if(file_exists("../photos/". $PicName)){
$PicMessage = "File Already Exists";
}
else{
move_uploaded_file($PicTempName, "../photos/". $PicName);
$PicMessage = "Stored in: ". "../photos/". $PicName;
}
}
}
else{
$PicMessage = "Invalid File Type";
}
图片消息显示文件已传输,但ptohos文件夹为空。 我真的想不出什么问题。
在此先感谢所有愿意花时间帮助我的人。
好的,我发现了问题所在。
in_array( $ extentions,$ allowedExt))){将其更改为$ extension。这是一个小错误,但足以延迟我们的工作。还可以删除add_slashes()并尝试。
尝试验证该文件实际上是通过POST请求上传的,因为正如该功能手册中所述: move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.
您可以使用is_uploaded_file()
函数验证文件是否通过PHP上传。 这将告诉您该文件是否甚至可以与move_uploaded_file()
。
将您的else
语句替换为:
else if(is_uploaded_file($PicName)) {
move_uploaded_file($PicTempName, "../photos/". $PicName);
$PicMessage = "Stored in: ". "../photos/". $PicName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.