PHP复制或移动上传文件 - 没有任何反应,但没有错误

时间:2011-08-22 18:49:02

标签: php file-upload

我没有收到任何错误,但我没有复制文件:

$upload_folder = "uploads/";

$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

$prefix = date("YmdHis");
$path_of_uploaded_file = "$upload_folder$prefix-$name_of_uploaded_file";
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}

echo $path_of_uploaded_file;
echo $name_of_uploaded_file;
echo $errors;

这在Windows开发环境中运行良好,但部署到Linux Web服务器就是这样做的。我们最初收到了复制错误,然后我们向uploads目录添加了权限。现在我们一无所获。

我也尝试过move_uploaded_file,没有错误,但上传目录中没有结果文件。

2 个答案:

答案 0 :(得分:6)

如果is_uploaded_file返回true,也许你可以添加一个检查。

if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
} else {
    $errors .= '\n error while uploading file'; // maybe  upload_max_filesize exceeded
// try to get the specific error

 switch($_FILES['uploaded_file']['error']){
    case 0: //no error; possible file attack!
      echo "There was a problem with your upload.";
      break;
    case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
      echo "The file you are trying to upload is too big.";
      break;
    case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
      echo "The file you are trying to upload is too big.";
      break;
    case 3: //uploaded file was only partially uploaded
      echo "The file you are trying upload was only partially uploaded.";
      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;

}

可能超出了upload_max_filesize,或者有其他服务器设置不允许上传。

请参阅php dokumentation了解有关可能出现问题的更多信息。

答案 1 :(得分:1)

$upload_folder = "uploads/";
$path_of_uploaded_file = "$upload_folder$prefix-$name_of_uploaded_file";

$path_of_uploaded_file成为

$path_of_uploaded_file = "$upload_folder/$prefix-$name_of_uploaded_file";