mkdir function not working properly - php

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

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...');
}
?>

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

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.

Permission denied on mkdir()

I'm getting the following error when trying to call mkdir() on a server...
Warning: mkdir() [function.mkdir]:
Permission denied in
/home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php
on line 373
The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.
How can I find out if the uploads folder is writable?
protected function category_images_base_dir()
{
// Where should the dir be? Get the base WP uploads dir
$wp_upload_dir = wp_upload_dir();
$base_dir = $wp_upload_dir[ 'basedir' ];
// Append our subdir
$dir = $base_dir . '/cat-images';
// Does the dir exist? (If not, then make it)
if ( ! file_exists( $dir ) ) {
mkdir( $dir ); //THIS IS LINE 373
}
// Now return it
return $dir;
}
is_writable() is probably the function you're looking for.
http://cz.php.net/manual/en/function.is-writable.php says:
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Also, the directly next line is relevant here:
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)
Make sure the parent folder is writable to the process that the web server runs as.
Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?

Categories