2

我在将 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";
}

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

非常感谢所有愿意花时间帮助我的人。

4

2 回答 2

2

好的。我发现了问题。

in_array( $ extentions , $allowedExt))){ 将其更改为$extension 。这是一个小错误,但足以延迟我们的工作。同时删除 add_slashes() 并尝试。

于 2012-12-26T03:38:56.687 回答
0

Try to verify that the file is actually uploaded with POST request, because as it says in the manual for this function: move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved. You can verify whether or not the file was uploaded through PHP with the function is_uploaded_file(). This will tell you whether or not the file is even capable of being used with move_uploaded_file().

Replace your else statement with:

else if(is_uploaded_file($PicName)) {
  move_uploaded_file($PicTempName, "../photos/". $PicName);
  $PicMessage = "Stored in: ". "../photos/". $PicName;
}
于 2012-12-26T03:02:53.197 回答