PHP 无法使用 mkdir 创建目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13908722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP unable to create a directory with mkdir
提问by Question Overflow
I have a previously working PHP script that is able to create a directory with mkdir:
我有一个以前工作的 PHP 脚本,它能够创建一个目录mkdir:
$webfolder = "/var/www/html/images/user";
mkdir($webfolder, 0770);
I made some changes to the permission setting of the folder /var/www/html/imageswhich is now:
我对文件夹的权限设置进行了一些更改,/var/www/html/images现在是:
drwxrwx---. myself apache system_u:object_r:httpd_sys_content_t:s0 images
I think previously this folder was owned by apache. But since apachehas the full privileges of read, write and execute as a user group, I wonder why it can't create a folder within. Using the mkdirproduces a falseboolean value.
我认为以前这个文件夹是由apache. 但是由于apache作为用户组具有读、写和执行的全部权限,我想知道为什么它不能在其中创建文件夹。使用mkdir产生一个false布尔值。
Is the problem due to directory ownership or is there some other reasons? Note that I am using PHP version 5.4.
问题是由于目录所有权还是有其他原因?请注意,我使用的是 PHP 5.4 版。
Error Log added:
错误日志添加:
[Mon Dec 17 11:12:34 2012] [error] [client 127.0.0.1] PHP Warning: mkdir(): Permission denied in /var/www/html/upload on line 33, referer: https://mywebsite.com/referer
[Mon Dec 17 11:12:34 2012] [error] [client 127.0.0.1] PHP 警告:mkdir():第 33 行 /var/www/html/upload 中的权限被拒绝,引用者:https://mywebsite。 com/referer
回答by Question Overflow
The answer is staring right in front of me, but I miss it due to my unfamiliarity with SELinux.
答案就在眼前,但由于我对 SELinux 不熟悉,我错过了它。
The SELinux context type should be set as httpd_sys_content_rw_tinstead of httpd_sys_content_tso that the folder is both readable and writable for apache. Changing the context recursively is done with the following command:
SELinux 上下文类型应设置为httpd_sys_content_rw_t而不是,httpd_sys_content_t以便该文件夹对 apache 可读可写。使用以下命令以递归方式更改上下文:
# chcon -R -t httpd_sys_content_rw_t /var/www/html/images
Good grief. Hope it helps others who come across this.
好伤心。希望它可以帮助遇到此问题的其他人。
回答by NBhat
On CentOS7 VM, with PHP5.4.16/Apache 2.4.6/mariadb 5.5.44, the smarty template directory was not writable to generate compiled templates files and was giving the below error (in /var/log/httpd/error_log):
在 CentOS7 VM 上,使用 PHP5.4.16/Apache 2.4.6/mariadb 5.5.44,smarty 模板目录不可写以生成编译模板文件,并给出以下错误(在 /var/log/httpd/error_log 中):
[Thu Mar 31 12:36:08.201383 2016] [:error] [pid 13094] [client 192.168.212.65:52204] PHP Fatal error: Smarty error: unable to write to $compile_dir '/var/www/html/app1/templates_c'. Be sure $compile_dir is writable by the web server user. in /var/www/html/app1/libs/smarty/Smarty.class.php on line 1093
hence the PHP application was displaying blank screen.
因此 PHP 应用程序显示空白屏幕。
chmod 777 templates_cdid not work either; but as per the suggestion by @Question Overflow, permission on web root on this VM did solve the problem.
chmod 777 templates_c也不起作用;但根据@Question Overflow 的建议,此 VM 上的 web root 权限确实解决了问题。
I had to execute:
我必须执行:
[root@appserver html]# chcon -R -t httpd_sys_content_rw_t /var/www/html
of course, the templates_c & cache should be owned by apache user:
当然,templates_c 和缓存应该归 apache 用户所有:
drwxr-xr-x. 2 apache apache 6 Mar 31 12:56 templates_c
drwxr-xr-x. 2 apache apache 6 Mar 31 12:56 cache
After spending more than half a day, came across this. Thanks
折腾了半天多,才发现这个。谢谢

