我正在将我的网站从共享主机迁移到 VPS。在我的 VPS 上进行测试时,我发现突然间我无法再通过网络表单上传文件了。
错误:
警告:move_uploaded_file(/uploads/logoklein.jpg):无法打开流:/var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php 中第 26 行警告中没有这样的文件或目录警告:move_uploaded_file (): 无法在第 26 行的/var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php 中将“/tmp/php01nhmx”移动到“/uploads/logoklein.jpg”
SO 上有很多关于此的问题,它们几乎都涉及正确设置权限。我将 TMP 和上传目录修改为 0777(不寒而栗),但无济于事。我知道 0777 不是明智之举,但我想确保这不是这些文件夹的权限问题。
由于唯一改变的是 VPS,我认为问题一定出在服务器上。我想也许网络服务器正在以“无人”身份运行,所以它无法访问 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 :(. }
我在这里错过了什么?
最佳答案
Louis Huppenbauer 在评论中对我的类似问题给出了正确的答案。
使用相对路径是行不通的,它需要一个完整路径。尝试将/uploads/更改为主机的完整目录,例如:D:\InetPub\Vhosts\website.com\website2.com\uploads\
关于php - move_uploaded_file 失败但权限似乎正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14522308/