Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Apache文件权限仅在/tmp中_Php_Apache_File Permissions - Fatal编程技术网

Php Apache文件权限仅在/tmp中

Php Apache文件权限仅在/tmp中,php,apache,file-permissions,Php,Apache,File Permissions,我正在使用CentOS 7和PHP7.2.5。我无法获取move\u upload\u file()、file\u put\u contents()或exec('cp/tmp/test.txt/var/www/html/test.txt',$out);在除/tmp以外的任何文件夹中工作/var/www/html归apache:apache所有,仅限权限为755。我试过777,但仍然被拒绝。它被卡在systemd-private-123abc-httpd.service-11cba/tmp/文件夹

我正在使用CentOS 7和PHP7.2.5。我无法获取move\u upload\u file()、file\u put\u contents()或exec('cp/tmp/test.txt/var/www/html/test.txt',$out);在除/tmp以外的任何文件夹中工作/var/www/html归apache:apache所有,仅限权限为755。我试过777,但仍然被拒绝。它被卡在systemd-private-123abc-httpd.service-11cba/tmp/文件夹中,但我为httpd禁用了PrivateTmp

上传的文件被上传,一切都很好,直到我尝试移动它。我可以将其移动到/tmp目录中的另一个文件名,但不能将其移动到任何其他目录

所有这些都很好:

file_put_contents('/tmp/test.txt', 'TTTTTEEEESSSSTTTTT!!!!');
exec('cp /tmp/test.txt /tmp/test2.txt', $out); 
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], '/tmp/'.$file);
所有这些都会发出警告:**:无法打开流:权限被拒绝

file_put_contents('/var/www/html/test.txt', 'TTTTTEEEESSSSTTTTT!!!!');
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], '/var/www/html/'.$file);
exec('cp /tmp/test.txt /var/www/html/test2.txt', $out); 
同样,/var/www/html归apache:apache所有,权限设置为755。我完全困惑不解。所有这些代码在旧服务器上运行良好,但在新服务器上绝对不起作用。我看了又看,但是如果答案在那里,那么它就会被太多关于标准文件/目录权限的答案所阻止


执行官('cp/…实际上不会给出警告或错误。它可以工作也可以不工作。它可以在/tmp目录中复制,但不能复制到其他任何地方。

正如上面的评论中所提到的,SELinux是问题所在。我禁用了它,一切正常。我不想关闭它,所以我尝试设置允许我需要的规则,如下所示。That给了我错误“semanage:error:unrecogned arguments:/var/www/html/test.txt”

我运行了semanagefcontext-l>output.txt并签出了该文件。已经有一条规则(适用于WordPress和/或Drupal)允许读取/写入/var/www/html中名为“uploads”的任何目录 /var/www/html(/)?/uploads(/)?所有文件系统\u:object\u r:httpd\u sys\u rw\u content\u t:s0

因此,我将目标目录移动到一个路径中有“uploads”的目录。 mv-f/var/www/html/shared/pics/*/var/www/html/shared/uploads/pics


我将其全部下载到apache:apache,并将权限设置为755,它可以按预期工作。因此,如果SELinux给您带来问题,而您无法更改规则,请查找规则并遵循它们。

如果您仍然有问题,请尝试禁用system privateTmp

我尝试过“multi-user.target.wants”解决方案,它已经工作了,但在重新启动后,PrivateTmp在某个时候又恢复为true。 就像Apache2的主要用途是PHP一样,我最终编辑了PHP.ini,并取消了sys_temp_dir行的注释

默认情况下,系统使用由函数sys_get_temp_dir分配的temp dir。函数sys_get_temp_dir将返回“/tmp”,但事实是,您的tmp文件存储在某个路径上,如/tmp/systemd-private-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-apache2.service-YYYYYY//tmp/*。因此,对我来说有效的是:

编辑php.ini(路径可以在php版本之间更改)

然后取消注释sys\u temp\u dir行

; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
sys_temp_dir = "/tmp"

您是否检查了SELinux是否已启用?cOle2-是的,这就是问题所在。详细信息见下面的答案。
; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
sys_temp_dir = "/tmp"