PHP mkdir is not setting permissions correctly [duplicate] - php

I'm trying to create a directory on my server using PHP with the command:
mkdir("test", 0777);
But it doesn't give full permissions, only these:
rwxr-xr-x

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.

The creation of files and directories is affected by the setting of umask. You can create files with a particular set of permissions by manipulating umask as follows :-
$old = umask(0);
mkdir("test", 0777);
umask($old);

Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file.
Example:
$dir = "test";
$permit = 0777;
mkdir($dir);
chmod($dir, $permit);

For those who tried
mkdir('path', 777);
and it did not work.
It is because, apparently, the 0 preceding the file mode is very important which tells chmod to interpret the passed number as an Octal instead of a decimal.
Reference
Ps. This is not a solution to the question but only an add-on to the accepted anwser

Probably, your umask is set to exclude those

In my case, I have to use the following way for centos7, which solved the problem
$oldmask = umask(000);//it will set the new umask and returns the old one
mkdir("test", 0777);
umask($oldmask);//reset the old umask
More details can be found at
https://www.php.net/manual/en/function.umask.php

Related

mkdir ("dir", 0777) and chmod ("dir", 077) not working

In short, the following code is meant to create a directory structure like:
>Attachments
>Lot
>Layer
The Attachments directory is fixed. The Lot comes out with 0777 permissions. The Layer directory does not. I added the chmod lines after concern that perhaps umask was at fault, but it didn't change anything.
// Create directory for this entry's attachments if needed.
$attachment_dir = $config_ini['OOCDB_defaults']['attachment_dir'];
$attachment_lot_dir = $attachment_dir.$txtLotID."/";
$attachment_lot_layer_dir = $attachment_lot_dir . $txtLayer."/";
if(!is_dir($attachment_lot_dir)){
mkdir($attachment_lot_dir , 0777);
}
if(!is_dir($attachment_lot_layer_dir )){
mkdir($attachment_lot_layer_dir , 0777);
}
chmod($attachment_lot_dir ,0777);
chmod($attachment_lot_layer ,0777);
$sleuthFile = $attachment_lot_layer_dir . "makeSleuthImg.txt";
$fp = fopen($sleuthFile,"w") or die("unable to open File! <br>");
//Write the string to the file and close it.
You have a typographical error:
$attachment_lot_layer_dir = $attachment_lot_dir . $txtLayer."/";
...
chmod($attachment_lot_layer ,0777);
That variable does not exist, so yes that will never work. PHP's mkdir respects umask in Linux (assuming you're on Linux otherwise this wouldn't be happening), so your directories are not being created at 0777 mask as requested; however chmod does not respect umask, so your first call to chmod is in fact changing this directory's mask to 0777. The second call is failing due to the bad variable name. Hence the behavior you're seeing.
FWIW, mkdir has a second optional, boolean parameter that will allow you to recursively create a directory structure in a single call by passing it the full directory path (see here). You should also look at this question to understand what to do with umask before calling mkdir if you want to avoid the subsequent calls to chmod entirely.

Warning: mkdir(): Permission denied

I am trying to make a directory when a new account is created.
The directory should be in my images folder and is used to better separate uploaded images.
//get the ID of the new account that was just created/inserted
$accountID = mysqli_insert_id($dbc);
//create a new directory path for that account
$directoryPath = "../images/" . $accountID;
// check if the directory exists
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
I had no problem getting this to work a few days ago, however when testing it today I am having problems.
I have included ini_set('display_errors', 'On'); in my page to see what error I am being thrown and it is a permission error.
Warning: mkdir(): Permission denied
The images folder has full read/write permissions for all users and groups as well as any parent folders so I don't understand how that would be an issue, that and it had worked several times before.
I am working on windows if that matters.
Any ideas?
To avoid spending too much time on permissions problems between the CLI user and the Apache user, an easy configuration is to use same user for both processes.
Get your user id and group by doing
$ id
uid=1000(my_user), gid=1000(my_group), ...
And then:
$ sudo service apache2 stop
$ sudo vi /etc/apache2/envvars
export APACHE_RUN_USER=my_user
export APACHE_RUN_GROUP=my_group
$ sudo chown -R my_user /var/lock/apache2
it's better and safer than to change you whole directory permission to 777
I think you should try this -
mkdir("../images/".$accountID, 0777, 'R');
Recursive folder creation may causing the problem.
Also get more information from - mkdir
Also check for folder permission.
You have to be sure that the parent directory allows you to create folder, and not the folder it self that is being created with 0777 rights...
Also, check with which user the Apache server is launched
Check if directory is already exist before using mkdir()
if (!is_dir ($directoryPath) ) {
mkdir($directoryPath, 0777);
}
I am working on windows if that matters.
It does.
Try changing this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath, 0777); //breaking here
}
To this
if (!is_dir($directoryPath)) {
//create the directory
mkdir($directoryPath); //breaking here
}
You are on a Windows box so it will ignore the chmod mode. Also try using the full paths and not relative.
The mode is 0777 by default, which means the widest possible access.
For more information on modes, read the details on the chmod() page.
Note:mode is ignored on Windows.
http://php.net/manual/en/function.mkdir.php

Mkdir permission doesn't set correct

I use this code to make folder in web site.
mkdir("folder/folder",0744,true);
But in practical "folder" permission is 600.
what's problem?
The mode may be affected by your current umask.
Do something like this:
$old_umask = umask(0);
mkdir('folder/folder', 0744);
umask($old_umask);

Why in PHP if you use Mkdir recursive flag do the nest directories not chmod?

I am using mkdir to create normally 2 nested directories for a file structure. The directories it creates are always set to 0755. The code I am using however is.
mkdir('path_one/path_two', 0777, true);
I have tried then doing
chmod('path_one/path_two', 0777);
but that only sets the final directory as 0777. What would cause mkdir not to function properly?
mkdir is functioning correctly. The intermediate directories created are set based on the current umask. You want something like:
umask(0777);
mkdir('path_one/path_two', 0777, true);
From the php manual:
The mode is also modified by the current umask, which you can change
using umask().
Note that any bits that are set in umask() are unset in the result that's used by mkdir(). The default umask is 0022 and the default create mode for mkdir is 0777, which gives a result value of 0755. This applies for all created directories.

Permissions with mkdir won't work

I can't understand why I have to use chmod to get the correct permissions..
The file is created succesfully but with 0755 and not 0775 that I specify in mkdir .
( http://php.net/manual/en/function.mkdir.php )
I have to do chmod after mkdir to set the correct permissions.
Safe mode is off in php.ini and the folder belongs to php's group and owner (www-data)
This doesn't work:
if(!is_dir("/var/www/customers/$username/$project_name"))
{
mkdir("/var/www/customers/$username/$project_name",0775);
}
But this does:
if(!is_dir("/var/www/customers/$username/$project_name"))
{
mkdir("/var/www/customers/$username/$project_name");
chmod("/var/www/customers/$username/$project_name",0775);
}
Yes, it's because of umask...
from comments of docs: http://php.net/manual/en/function.mkdir.php
You might notice that when you create
a new directory using this code:
mkdir($dir, 0777);
The created folder actually has
permissions of 0755, instead of the
specified
0777. Why is this you ask? Because of umask(): http://php.net/manual/en/function.umask.php
The default value of umask, at least
on my setup, is 18. Which is 22 octal,
or
0022. This means that when you use mkdir() to CHMOD the created folder to
0777, PHP takes 0777 and substracts
the current value of umask, in our
case 0022, so the result is 0755 -
which is not what you wanted,
probably.
The "fix" for this is simple, include
this line:
$old_umask = umask(0);
Right before creating a folder with
mkdir() to have the actual value you
put be used as the CHMOD. If you would
like to return umask to its original
value when you're done, use this:
umask($old_umask);
I think you may have to modify your umask.
As noted on the mkdir manpage:
The mode is also modified by the current umask, which you can change using umask().
Now, looking at the umask() manpage, one of the comment listed confirms my inner thought:
"It is better to change the file permissions with chmod() after creating the file."
In other words, I believe the way you are doing it is more secure:
Set your umask so that files are created private to your user, then use chmod to open them up.
Try calling this function before you make your directory: clearstatcache(); Also, maybe you should check if you can do it using just mkdir if you siwtch to another user.

Categories