使目录权限被拒绝,但权限设置正确

时间:2017-06-18 15:49:23

标签: php linux apache centos centos7

我正在尝试创建一个简单的php脚本,在/ var / www / html中创建一个目录。代码是:

$dir = '/var/www/html/testdir';

 if ( !file_exists($dir) ) {
     $oldmask = umask(0);  // helpful when used in linux server
mkdir($dir, 0777, true) || chmod($dir, 0777);
     mkdir ($dir, 0744);
 }

问题是,执行此操作时出现权限错误。但是html目录的权限是:

drwxrwxrwx. 7 apache apache 4096 Jun 18 11:46 html

这是错误:

Warning: mkdir(): Permission denied in /var/www/html/createdir.php on line 6

另外,<?php echo exec('whoami'); ?>告诉我php正在运行为apache。

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。 Linux阻止Apache创建目录。

修正:

chcon -R -t httpd_sys_content_t /path/to/www
chcon -R -t httpd_sys_content_rw_t /path/to/www/dir/for/rw