2013-04-29 183 views
2

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

以下是处理文件上传的代码片段。我无法弄清楚我的条件有什么问题。任何想法,将不胜感激!

 // 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; 
     } 
+0

您可以分享完整的$ _FILES数组,因为错误参数中的值可能有助于阐明 – fullybaked 2013-04-29 16:05:56

+0

这里是$ _FILES数组输出:Array([new_file] => Array([name] => sample .doc [type] => msword/doc [tmp_name] =>/tmp/phpsTu20P [error] => 0 [size] => 119674))想法? – kthaker 2013-04-29 16:25:15

+0

错误参数中的值为0. – kthaker 2013-04-29 16:28:25

回答

0

如果$_FILES['new_file']['error'] == 0然后上传是没有问题的,但调用move_uploaded_file()是。您可能对试图将文件移动到的目录拥有不正确的权限。

0

您确定要上传的文件夹是否具有写入文件的权限?如果不是,请使用chmod 0777并使用它进行测试。

+0

是的,它具有chmod 775权限。 – kthaker 2013-04-29 16:25:50

0

您的目标文件夹是否具有适当的权限? http://en.wikipedia.org/wiki/Chmod目录写入通常需要775:What are the proper permissions for an upload folder with PHP/Apache?

另外,你是否希望用户能够直接访问该文件?如果不是,您应该考虑将文件写入Web根目录之上的文件夹。

+0

我再次检查权限。他们是775.是的,我确实希望用户能够直接访问该文件。我认为这个问题可能在'move_uploaded_file'这一行。数据库将文件名存储在表中,并在上载后将其显示在表单中。但是当我检查上传文件夹时,文件不存在。 – kthaker 2013-04-29 16:27:28

相关问题