Php 移动\u上载的\u文件失败,但权限似乎正确
我正在将我的网站从共享主机移动到VPS。在我的VPS上测试时,我注意到突然间我无法通过网络表单上传文件了 错误:Php 移动\u上载的\u文件失败,但权限似乎正确,php,linux,apache,plesk,Php,Linux,Apache,Plesk,我正在将我的网站从共享主机移动到VPS。在我的VPS上测试时,我注意到突然间我无法通过网络表单上传文件了 错误: 警告:移动上传的文件(/uploads/logoklein.jpg):无法打开流:第26行的/var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php中没有此类文件或目录警告:移动上传的文件():无法将“/tmp/php01nhmx”移动到/var/www/vhosts/denkproducties.nl/
警告:移动上传的文件(/uploads/logoklein.jpg):无法打开流:第26行的/var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php中没有此类文件或目录警告:移动上传的文件():无法将“/tmp/php01nhmx”移动到/var/www/vhosts/denkproducties.nl/httpdocs/denkproducties/upload.php第26行的“/uploads/logoklein.jpg”
关于这一点有很多问题,它们几乎都涉及到设置正确的权限。我将TMP和uploads目录都添加到0777(抖动),但没有任何效果。我知道0777不是一件明智的事情,但我想确保这不是这些文件夹的权限问题
由于唯一改变的是VPS,我想问题一定出在服务器上。我想可能是web服务器以“无人”身份运行,所以它无法访问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
为了完整起见,我用一个web表单制作了一个小的上传脚本:
$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 :(.
}
$allowed_filetypes=array('.jpg'、'.gif'、'.bmp'、'.png');//这些将是通过验证的文件类型。
$max_filesize=1024000;//以字节为单位的最大文件大小(当前为1MB)。
$upload_path='/uploads/';//文件将上载到的位置(当前为“上载”目录)。
$filename=$_文件['userfile']['name'];//获取文件名(包括文件扩展名)。
$ext=substr($filename,strpos($filename,“.”),strlen($filename)-1);//从文件名中获取扩展名。
//检查文件类型是否允许,如果不允许,请通知用户。
如果(!in_数组($ext,$allowed_文件类型))
die('不允许您尝试上载的文件');
//现在检查文件大小,如果文件太大,请退出并通知用户。
if(文件大小($\u FILES['userfile']['tmp\u name'])>$max\u文件大小)
die('您尝试上载的文件太大');
//检查是否可以上传到指定的路径,如果没有死,通知用户。
//如果(!是可写的($upload\u path))
//die('您无法将指定目录上载到'$upload_path',请将其CHMOD到777');
//将文件上载到指定路径。
如果(移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\$filename)){
echo“您的文件上载成功,请在此处查看文件”;//成功。
}否则{
echo“上载文件时出错。请重试”。;//失败:(。
}
我在这里遗漏了什么?路易斯·赫彭鲍尔在评论中对我的类似问题给出了正确的答案
使用相对路径不起作用,它需要完整路径。请尝试更改/上载/到主机的完整目录,例如:D:\InetPub\Vhosts\website.com\website2.com\uploads\echo$upload\u path。$filename并检查文件路径是否正确。它会回显/uploads/logklein.jpg,这是正确的路径和文件名。目录是否为
upload/
丢失?目录upload/是否存在,当前编码为0777…删除upload\u path
变量中的第一个/
(或将其替换为绝对路径,如/var/www/vhosts/example.com/httpdocs/upload/
).PHP可能试图将文件移动到文件夹/upload
(unix文件结构根文件夹)中,而不是移动到apache文件夹中。