PHP复制或移动上传的文件-没有发生任何事情,但没有错误


PHP copying or moving uploaded file - nothing happens, but no error

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

$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服务器上就是这样做的。我们最初得到一个复制错误,然后我们添加权限到上传目录。现在我们什么也得不到。

我也试过这个与move_uploaded_file,没有错误,但没有结果文件在上传目录

也许您可以添加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文档了解更多可能出现的问题

$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";