Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unable to create a directory with mkdir

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

like image 446
Question Overflow Avatar asked Dec 17 '12 05:12

Question Overflow


People also ask

How do you fix mkdir Cannot create directory?

Resolving The Problem Simply log in as super user “su” and use “chmod 777” to set the directory permissions of where you wish the rational directory to be created. Once done, you can re-enter the original directory again and the install will continue using the same directory.

Why does mkdir not create directory?

mkdir: cannot create directory – Permission denied The reason for this error is that the user you're running the mkdir as, doesn't have permissions to create new directory in the location you specified. You should use ls command on the higher level directory to confirm permissions.

Why is mkdir permission denied?

[ErrorException] mkdir(): Permission denied That means you do not have write permission on your project folder. Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project.

How create directory if not exists in PHP?

Methods: file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not. is_dir(): It is also used to check whether a file or directory exists or not. mkdir() : This function creates a directory.


1 Answers

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.

like image 51
Question Overflow Avatar answered Oct 20 '22 07:10

Question Overflow