文件上传(工作在本地,但不能在远程服务器上)


File Upload (works locally, but not on remote server)

我试图通过HTML表单将文件(msword/doc)上传到Apache服务器文件夹。当我在本地进行测试时(我通过MAMP进行测试),它可以工作,但是当我将它上传到远程服务器(例如GoDaddy)时,它不起作用。它显示"文件上传有问题"。

下面是处理文件上传的代码片段。我不知道我的条件句出了什么问题。

      // Move the file to the target upload folder
      $target = FILE_UPLOADPATH . basename($new_file);
      if (move_uploaded_file($_FILES['new_file']['tmp_name'], $target)) 
      {
        // The new file move was successful, now make sure any old file is deleted
        if (!empty($old_file) && ($old_file != $new_file)) 
        {
          @unlink(FILE_UPLOADPATH . $old_file);
        }
      }
      else 
      {
        // The new file move failed, so delete the temporary file and set the error flag
        @unlink($_FILES['new_file']['tmp_name']);
        echo 'There was a problem with the file upload.' . PHP_EOL;
      }

您确定要上传的文件夹有写入文件的权限吗?

您的目标文件夹是否具有适当的权限?http://en.wikipedia.org/wiki/Chmod目录写入通常需要775:PHP/Apache上传文件夹的适当权限是什么?

另外,您是否希望用户能够直接访问该文件?如果没有,你应该考虑把文件写到根目录上面的一个文件夹里。

如果$_FILES['new_file']['error'] == 0,那么上传不是问题,但对move_uploaded_file()的调用是。

您可能对要移动文件的目录的权限不正确。

对于我来说,我碰巧在php的upload_max_filesize下使用文件进行本地测试,而在upload_max_filesize上使用文件进行远程测试。详见https://stackoverflow.com/a/30359278/3325776

相关文章: