PHP unable to create a directory with mkdir

Question Overflow picture Question Overflow · Dec 17, 2012 · Viewed 28.2k times · Source

I have a previously working PHP script that is able to create a directory with mkdir:

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

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

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

I think previously this folder was owned by apache. But since apache has the full privileges of read, write and execute as a user group, I wonder why it can't create a folder within. Using the mkdir produces a false boolean value.

Is the problem due to directory ownership or is there some other reasons? 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, referer: https://mywebsite.com/referer

Answer

Question Overflow picture Question Overflow · Dec 18, 2012

The answer is staring right in front of me, but I miss it due to 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 both readable and writable for apache. Changing the context recursively is done with the following command:

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

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