Creating writable directory with mkdir in PHP - php

I have this code to create recursive directories:
define('_PATH', '/www/virtual/user/galleries');
$gallery_path = "/".intval($gallery['id']);
if(!is_dir(_PATH.$gallery_path))
mkdir(_PATH.$gallery_path, 0777, false);
I made the parent directory ('galleries') writable (0777).
I can confirm that the directory is created. However, it is NOT created with 0777 permissions, but with 0755! I have no explanation for this. :-(

Related

php mkdir will only work when creating a folder inside a 777 folder

I have a folder called 'folders'. I want to create a folder inside 'folders' called 'folder' with mkdir()
However the folder fails to create when 'folders' is set to 755. The only may I can get mkdir to work is making 'folders' 777. Is this typical or is there something else wrong? Shouldn't be able to do that on 755, isn't 777 a security risk?
A 777 permission on the directory means that everyone has access to read/write/execute (execute on a directory means that you can do a ls of the directory).
Try this one below if you fails to create folder inside the folder.
<?php
// Desired folder structure
$structure = './folders/folder/';
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0755, true)) {
die('Failed to create folders...');
}
?>

mkdir function not working properly

I have folder hierarchy as Bucharest/Waterfall/a.php. My code snippet for making directory on server is as follows:
if(!is_dir($this->folder)){
$old_umask = umask(0);
mkdir($this->folder, 0777);
umask($old_umask);
}
chmod($this->folder, 0777);
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile))
return true;
All files are uploaded to the server. Now the issue is that the parent folder ,i.e. Bucharest has permission 755 while inner folder has permission 777. $this->folder has value as Bucharest/Waterfall. It gives 755 permission to Bucharest while 777 permission to waterfall. According to my code the 777 permission should also be given to Bucharest.
I have also tried chmod but all in vain. I want to give full permission to parent folder.
is_dir($this->folder); tells you that the file isn't a directory, not that it doesn't exist. and of course you can't mkdir if the directory is already there.
Also, if the directory already exists, according to the documentation chmod will not do anything if the user under which php is running isn't the same as the user who owns that directory.
Is the folder you are checking a subfolder already? If so, create the parent folders firts.
i.e.
mkdir('/tmp/test1/test2/test3/test4');
will fail if '/tmp/test1/test2/test3' doesn't exist

How to create folder in parent directory?

My php script is located in /var/www/html/users/dev. I need to create a folder in /var/www/images/ - something like /var/www/images/test/test/ and store here some images.
But when i trying is with mkdir($file_dir, 0777); where $file_dir is /var/www/images/test/test/ i receive an error:
Warning: mkdir(): No such file or directory in /var/www/html/users/dev/classes/sites.class.php...
Because "/var/www/images/test" does not exists, so you can not mkdir("/var/www/images/test/test")
You can specify the "$recursive" to TRUE, and it will work, like this:
mkdir($file_dir, 0777, TRUE);
Try
mkdir($file_dir, 0777, true);
The third parameter ('recursive') allows you to specify a path of which all directories will be created. If you don't, only the last directory ('test') will be created, and the whole path before that must exist.
The PHP documentation is quite clear about that.
if it is Linux you have set permissions to your parent directory 1st.
sudo chmod -R 777 /path of ur directory.

PHP create directory with 777 permission in Windows

I have read this:-
Why can't PHP create a directory with 777 permissions?
and I can see a new folder being created by applying the following:-
// Desired folder structure
$structure = "../../../".$flash_dir."HELLO";
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
$oldmask = umask(0);
mkdir($structure, 0777);
umask($oldmask);
when viewing the file permission of HELLO with DreamWeaver, it is 777. However, I suspect it is a Linux 0777 rather than a Windows 777, therefore I still cannot upload things to HELLO.
Will there be any alternative method to create a directory with windows 777? Thanks!
PS. when I manual create a new directory and right click it to set 777, it works perfectly, so I really think it's related to Linux vs 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);

creating folders and permissions in linux

I wanted to create a folder and set its permission.
It was working fine with windows, but when I shifted to linux,
it had permission problems.
//Make new directory
$directory = dirname($this->fileName);
if(!is_dir($directory)) {
if (!mkdir($directory, 0777, true))
die('Failed to create folders...');
} else {
die('ah ok...');
}
For this to work, I have to set the folder permission manually to 777.
Then after that the application can run successfully.
Then I delete the existing folder again to test it, cannot create again because
permission denied, it fails to create folder.
Check your umask() and set it to 0. If your umask is 0022 for example, your newly created directory will have permission 0755 instead of 0777 when you create it.

Categories