is_uploaded_file表示错误但没有返回错误代码

时间:2013-10-07 07:14:03

标签: php file-upload

我有以下代码:

 if (!is_uploaded_file($_FILES['sb_file']['tmp_name']))
                           {
                               echo "Error in Uploading File <br>".$_FILES["sb_file"]["error"][$count];
                           }

is_uploaded_file返回false,即使$ _FILES ['sb_file'] ['errors']返回0但文件未在目标文件夹中移动。如何追踪错误?

foreach ($_FILES['sb_file']['name'] as $filename)
        {
            //make sure no empty thing goes
            if($filename!=""&&$filename!=null)
            {
                $temp = explode(".", $filename);
                $extension = end($temp);
                $upload_folder = "/home/content/w/k/1/wk1989/html/".trim($config["UPLOAD_FOLDER"]);
                 if(!(in_array($extension, $allowedExts)))
                    print "$filename has Invalid file Type <br />";
                 else
                 {
                     $uploaded_file_name = $filename."-".date("YmdHis").".".$extension;
                     if ($_FILES["sb_file"]["error"][$count] > 0)
                        {
                            echo "<br />Error: " . $_FILES["sb_file"]["error"][$count] . "<br />";
                        }
                        else
                        {
                            $target_path = "uploads/";
                            $target_path = $target_path.basename( $file_upload['name']); 
                            print "Path = $target_path <br>";
//                            if(move_uploaded_file($file_upload['tmp_name'], $target_path)) {
//                            //    echo "The file ".  basename( $file_upload['name']). " has been uploaded";
//                            } else{
//                                //echo "There was an error uploading the file, please try again!";
//                            }
                           move_uploaded_file($_FILES["sb_file"]["tmp_name"][$count],$upload_folder.$uploaded_file_name);
//                           if (!is_uploaded_file($_FILES['sb_file']['tmp_name'][$count]))
//                           {
//                               echo "Error in Uploading File <br>".$_FILES["sb_file"]["error"][$count];
//                           }
//                           else
//                           {
//                               
//                           }
                           $is_upload = true;
                           echo "Uploaded = ".$upload_folder.$uploaded_file_name;

                        }
                        if($is_upload)
                        {
                            $uploaded_files[] = $upload_folder.$uploaded_file_name;
                        }
                 } 
            }
             $count++;
        }

1 个答案:

答案 0 :(得分:1)

首先,我建议您使用move_uploaded_file而不是手动移动文件。 要调试当前代码,您应该执行print_r($_FILES)并检查路径是否正确以及有关该文件的信息是否已发送到您的脚本。

我发现帖子说函数is_uploaded_file处理区分大小写的文件名(即使在Windows上)(read the original post)。

我认为处理上传文件的最佳方式是这样的想法:

$upload_dir = "D:/user_uploads/";
foreach($_FILES as $key => $file_arr)
{
  switch($file_arr["error"])
  {
    case UPLOAD_ERR_OK:
    {
      if($file_arr["size"]>0)
      { 
        $result = move_uploaded_file($file_arr["tmp_name"], $upload_dir.$file_arr["name"]);
        if($result === true) echo "Upload ok";
        else                 echo "Upload failed";
      }
      else echo "The file has no content";
    }
    default: 
    {
      echo "Upload Error #".$file_arr["error"]." for field" . $key;
      print_r($files_arr); // only for debuging, remove this on live systems
    }
  }
}

如果需要,可以为例如添加更多案例块UPLOAD_ERR_FORM_SIZE。在 Unix / linux 或ntfs驱动器上,您应该检查upload_dir is_writeable()