PHP cannot create a directory with mkdir

I have a previously working PHP script that can create a directory with mkdir :

 $webfolder = "/var/www/html/images/user"; mkdir($webfolder, 0770); 

I made some changes to the permission setting for the /var/www/html/images folder, which is now:

 drwxrwx---. myself apache system_u:object_r:httpd_sys_content_t:s0 images 

I think this folder previously belonged to apache . But since apache has full read, write, and execute privileges as user groups, I wonder why it cannot create a folder inside. Using mkdir creates a boolean false .

Is the problem due to directory membership or are there other reasons? Please note that I am using PHP version 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, referar: https: // mywebsite. com / referer

+10
php apache permissions mkdir selinux
Dec 17 '12 at 5:25
source share
2 answers

The answer is looking right in front of me, but I miss it because of my unfamiliarity with SELinux.

The SELinux context type should be set as httpd_sys_content_rw_t instead of httpd_sys_content_t so that the folder is read and written to apache. Changing a recursive context is done using the following command:

 # chcon -R -t httpd_sys_content_rw_t /var/www/html/images 

Good grief. Hope this helps others who come across this.

+26
Dec 18 '12 at 12:53
source share

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 template files and gave the following error (in / 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 

therefore, the PHP application displayed a blank screen.

chmod 777 templates_c did not work either; but as per @Question Overflow's suggestion, the web root permission on this virtual machine resolved the issue.

I had to do:

 [root@appserver html]# chcon -R -t httpd_sys_content_rw_t /var/www/html 

Of course, templates_c and cache should belong to apache user:

 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 noon, I came across this. Thanks

0
Mar 31 '16 at 7:52
source share



All Articles