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.
Related
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
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.
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.
The following works in windows:
mkdir('../my/folder/somewhere/on/the/server', 0777, true);
I am talking about PHP mkdir.
It works perfectly, and creates the subfolders recursively. However, if I run the same command on a linux server, the folders aren't created.
Previously I solved this by breaking up the path and creating each folder one by one. But I don't want to do that because it should work with the "resurive" flag set to true. Why isn't it working?
Sorry, but there must be some problem apart from the mkdir command itself.
This tiny example works as expected and recursively creates the directories for me when executed on Linux:
#!/usr/bin/php
<?php
mkdir ('testdir/testdir2/testdir3',0777,TRUE);
?>
This are the thing have discovered
Make sure the root path exists
Make sure the root path is writable
Don't use .. always use real path ...
Example
$fixedRoot = __DIR__;
$recusivePath = 'my/folder/somewhere/on/the/server';
if (is_writable($fixedRoot) && is_dir($fixedRoot)) {
mkdir($fixedRoot . DIRECTORY_SEPARATOR . $recusivePath, 0, true);
} else {
trigger_error("can write to that path");
}
Make sure that your PHP user (eg www-data) has permission to write to the parent folders of any folder it is trying to create. PHP needs to be able to write to the lowest one that already exists.
For example, in the case of ../my/folder/somewhere/on/the/server, if ../my already exists and PHP is able to write to .. but not to my, mkdir will fail.
If your user is www-data, you could use sudo chown -R www-data:www-data ../my to give write permission for my and all its subfolders.
For a project I'm implementing a file-upload system. For every user account I would like the script to create a different sub-folder. Lets say their user_id's.
Each time a user is added, the system will create a new sub-folder for their own uploads. For example:
Uploads/
- user1
- user2
- user3
By executing mkdir('Uploads/'.$user_id, 0777); it will create a new subfolder. Everything is fine.
However my application is not able to write to this folder. How do I have php make directories with the required file permissions? I have tried using chmod with no success.
This might help chmod and mkdir
$dirMode = 0777;
mkdir($directory, $dirMode, true);
// chmod the directory since it doesn't seem to work on recursive paths
chmod($directory, $dirMode);
For mkdir, mode is ignored on Windows. and 0777 is by default. and the third param is recursive which allows the creation of nested directories specified in the pathname.
sometimes the directory created with another mode than specified ( 0755 instead 0777 etc).
to solve that use :
<?php
$old = umask(0);
mkdir($dir,0777);
umask($old);
?>