move_uploaded_file失败,但权限似乎正确

时间:2013-01-25 12:59:50

标签: php linux apache plesk

我正在将我的网站从共享主机移动到VPS。在我的VPS上进行测试时,我注意到突然间我无法通过网络表单上传文件了。

错误: Warning: move_uploaded_file(/uploads/logoklein.jpg): failed to open stream: No such file or directory in /var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php on line 26 Warning: move_uploaded_file(): Unable to move '/tmp/php01nhmx' to '/uploads/logoklein.jpg' in /var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php on line 26

在SO上有很多关于此的问题,它们几乎都涉及设置权限。我将TMP和uploads目录都修改为0777(不寒而栗),但无济于事。我知道0777不是一件好事,但我想确保这些文件夹不是权限问题。

由于唯一改变的是VPS,我认为问题必须在服务器上。我想也许Web服务器运行为'nobody',因此它无法访问tmp文件夹。我跑了:

ps aux | grpe httpd

这告诉我:

root   27371   0.0  2.3   340860   24726  ?   SNs   12:57   0:00   /usr/sbin/httpd
apache 27372   0.0  0.9   240994    9820 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27373   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27374   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27375   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27376   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27377   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
apache 27378   0.0  1.6   341671   16700 ?    SN    12:57   0:00   /usr/sbin/httpd
root   27817   0.0  0.0   103244     824 tty1 S+    13:40   0:00   grep httpd

顺便说一下,我的服务器正在运行fCGI。

为了完整起见,我使用网络表单制作了一个小型上传脚本:

$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
   $max_filesize = 1024000; // Maximum filesize in BYTES (currently 1MB).
   $upload_path = '/uploads/'; // The place the files will be uploaded to (currently a 'uploads' directory).


   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   //if(!is_writable($upload_path))
   //   die('You cannot upload to '. $upload_path .'the specified directory, please CHMOD it to 777.');

   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)){
         echo 'Your file upload was successful, view the file here'; // It worked.
     } else {
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
 }

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

Louis Huppenbauer在评论中对我的类似问题给出了正确的答案。

使用相对路径无效,它想要一条完整的路径。尝试更改/上传/到主机的完整目录,例如:D:\ InetPub \ Vhosts \ website.com \ website2.com \ uploads \