Move_uploaded_file()函数在引用另一个页面时不起作用


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",其中引用了存储文件的部分:第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";
}

图片消息显示文件已传输,但phos文件夹为空。我实在想不出出了什么问题。

提前感谢所有花时间帮助我的人

好的。我发现问题了。

in_array($ extensions , $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;
}