我在CentOS上使用Apache 2.4和PHP 7遇到此错误。
我有这样的文件夹结构:
var
-- www
-- html
-- upload.php
-- uploads
-- images
当我运行move_uploaded_file($ tmpDir,'/ var / www / html / uploads'); -一切正常。
但是,当我尝试将目标目录更改为/ var / www / html / images时,就会出现错误。
Warning: move_uploaded_file(/var/www/html/images/test.jpg): failed to open stream:
Permission denied in /var/www/html/upload.php on line 14
Warning: move_uploaded_file(): Unable to move '/tmp/phps3D2Em' to
'/var/www/html/images/test.jpg' in /var/www/html/upload.php on line 14
两个文件夹具有相同的755模式和相同的所有者(apache)。 但是当我想在图像文件夹中上传时,它以某种方式给了我错误。
我从PHP运行posix_getuid(),它为我提供了Apache用户的ID。
可能是什么问题?
编辑:
ls -lh / var / www / html的结果:
-rw-r--r--. 1 root root 2.6K Mar 10 13:02 upload.php
drwxr-xr-x. 5 apache root 23 Mar 12 16:06 images
drwxr-xr-x. 2 apache root 88K Mar 13 12:36 uploads
httpd -S的结果:
ServerRoot:"/etc/httpd"
Main DocumentRoot:"/var/www/html"
Main errorLog:"/etc/httpd/logs/error_log"
Mutex authdigest-client: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
PidFile:"/run/httpd/httpd.pid"
Define: _RH_HAS_HTTPPROTOCOLOPTIONS
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48
编辑2:
我在/var/www/html/perms.php上设置了以下脚本:
print posix_getuid();
$dirs = array_filter(glob('*'), 'is_dir');
foreach( $dirs as $dir ) {
if ( is_writable($dir) ) print"{$dir} is writable.";
else print"{$dir} is not writable.";
}
和输出:
48
images is not writable. Permissions may have to be adjusted.
uploads is writable.
photos? images? 哪有
照片!=图片
抱歉,它的图像。 香港专业教育学院更新了问题。
我通过键入以下命令解决了该问题:
sudo chcon -t httpd_sys_rw_content_t /var/www/html/images -R
您能否分享以下结果:
ls -lh /var/www/html
另外,按照以下步骤共享apache用户:https://serverfault.com/a/802350/429496
Ive更新了ls-lh的结果。 由于我安装了systemctl,因此apachectl -S对我不起作用。 我不知道哪个命令适用于systemctl httpd。
您必须安装Apache 2。 使用apache2ctl代替apachectl
apache2ctl还是没有,我用httpd -S解决了,我用结果更新了问题。
是。 你是对的。 我刚刚看到您正在使用CentOS。 我以某种方式认为您拥有Ubuntu。 可能我混了两个不同的帖子。
您可以共享代码吗?
我已经为文件夹权限检查设置了自定义脚本,请参阅我的问题的"编辑2"。
您还可以共享ls -lh varwwwhtmlimages的结果吗?
另外,能否请您共享文件的0-20行:/var/www/html/upload.php
我通过键入以下命令解决了此问题:sudo chcon -t httpd_sys_rw_content_t / var / www / html / images -R。