Why Can't PHP Create a Directory with 777 Permissions

Why can't PHP create a directory with 777 permissions?

The mode is modified by your current umask, which is 022 in this case.

The way the umask works is a subtractive one. You take the initial permission given to mkdir and subtract the umask to get the actual permission:

  0777
- 0022
======
0755 = rwxr-xr-x.

If you don't want this to happen, you need to set your umask temporarily to zero so it has no effect. You can do this with the following snippet:

$oldmask = umask(0);
mkdir("test", 0777);
umask($oldmask);

The first line changes the umask to zero while storing the previous one into $oldmask. The second line makes the directory using the desired permissions and (now irrelevant) umask. The third line restores the umask to what it was originally.

See the PHP doco for umask and mkdir for more details.

Why can't PHP create a file, even with 777 permissions?

PHP has a directive named open_basedir which permit to limit the access on filesystem to specified directory-tree.

For example if you have in your php.ini:

open_basedir = /srv/http/:/home/:/tmp/

and you will access the file /filepath/filename.php, then you must set:

open_basedir = /srv/http/:/home/:/tmp/:/filepath/

otherwise PHP will not be able to access the file.

In this way that will allow all PHP code running on this machine to access /filepath/ directory-tree, so it is not secure for production environment. The better way is to set virtual-host based open_basedir in httpd.conf. I let you check your HTTP server documentation and also open_basedir documentation for that.

PHP create directory with 777 permission in Windows

0777 is exactly the same thing as 777

But I still can't say what the problem is. I would try to chmod it again after you've created it.

$oldmask = umask(0);
chmod($structure, 0777);
umask($oldmask);

PHP Cannot write to file even though it has 777 permissions

Telanor, AFS is a very big clue.

AFS (Andrew File System) has a whole level of directory permissions beyond that of traditional unix filesystems. Check AFS permissions on the directory to make sure you have the ability to access files in the directory. Also it may not be your permissions that matter, but the permissions of the webserver (or rather the userid it's running under). It's been a long time since I used AFS so I don't know the commands offhand to check directory permissions.

PHP can open file only with 777 permissions

PHP running under www-data user. After "chown" to that user, all is worked as it should

Change folder permission to 777 using PHP temporarily

PHP provides a function, chmod() for the task.

Attempts to change the mode of the specified file to that given in mode.

You can put it in an if statement, and if it returns false, you can skip the upload file part.


The usage will be like

if( chmod($path, 0777) ) {
// more code
chmod($path, 0755);
}
else
echo "Couldn't do it.";

As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)



Related Topics



Leave a reply



Submit