Why is it impossible to create this directory? - php

I want to create a directory in a Linux system. The parent directory which will contain the newly created directory has a 777 permission. The problem is that if the new directory doesn't include a subdirectory then it is created successfully. But when it contains a subdirectory , for example docs/photos then the subdirectory ( here /photos ) is not created ! So how to make the subdirectory created ?
Here is the codes :
if (!file_exists($rep))
{
if (mkdir($rep))
{
move_uploaded_file($_FILES['upload_field']['tmp_name'] , $rep."/"."photo".$image_id.".png");
}
}

The documentation for mkdir indicates that there is a $recursive parameter you should use for this.
recursive
Allows the creation of nested directories specified in the pathname. Defaults to FALSE.
Read the docs for usage.

By default, mkdir does not recursively create subdirectories. If you want such a behavior, you have to add the recursive parameter: mkdir($rep, 0777, true).See the doc for more details.
(By the way, you should avoid 777 permissions, they very often cause security issues.)

There's a third parameter, you have to set to TRUE to make it work. This parameter is for recursive. Otherwise PHP will only create the "root" dir
http://de.php.net/mkdir

Related

Error creating directory using mkdir function in Codeigniter

I'm trying to recursively create a directory using php's mkdir function in a Codeigniter installation. My code looks like this:
mkdir('docs/client/bills/payd', 0777, true)
The docs directory already exists in my site root dir, the client directory is beeing created with 0755 permission, the bills directory is beeing created with permission 1341 (weird!) and the last directory, payd, is never created.
I tryed to change permission in the mkdir argument list to 0, 755, etc... and nothing has changed. I also tryed to set umask to 0, 0777... and nothing.
umask(0777);
mkdir('docs/client/bills/payd', 0777, true)
Can anyone please say what am I doing wrong? The code above is called from a Codeigniter regular controller.
Try with
if ( ! is_dir( FCPATH.'docs/client/bills/payd' )//FCPATH is absolute path to the project directory
{
mkdir( FCPATH.'docs/client/bills/payd', 0777, true );//although 0755 is just fine and recomended for uploading and reading
}
Use this to specify it the working directory, it might be confused as to where the directory is located.
mkdir( getcwd().'docs/client/bills/payd', 0777, true);
getcwd is the working directory for your codeigniter. You can search in the PHP guide the getcwd() function to make it clearer.
This should work.
EDIT
To make it clearer, that would return the following:
C:\xampp\htdocs\YOUR_ROOT_DIRECTORY\docs\client\bills\payd
EDIT AGAIN
Although after reading again, this would only create payd and assume that docs\client\bills is already created. You could create client and bills using mkdir or using the file explorer. But there are other PHP ways and I can help if needed.
Goodluck meyt
I also had this weird "1341" permissions error with PHP mkdir, nothing to do with CodeIgniter, it's a pure PHP issue!
After much experimentation, the only way I could get it to work was to include a slash at the end of the path, and set the recursive flag to 'true'. (Even though the PHP docs don't show a final slash, and I was only creating a single directory.)
I.e.
$existing_path = '/these/directories/already/exist/';
mkdir( $existing_path . 'new-directory/', 0755, true);

PHP create multiple directories

Let's say, in PHP, I am trying to put an image in a specific directory that is on root.
I want to put it on /images/afc/esp/stadium/ directory. Images folder, Federation folder, Country ISO3 folder, content folder.
$folder_full = "images/".$getFed."/".$country_folder."/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full);
Before you ask, yes $getFed and $country_folder work and output text. So, then I get this error: Warning: mkdir(): No such file or directory
I don't get it?
Some of your subdirectories don't exist so you either need to iteratively create them or set the 3rd argument to mkdir() to true. Note that the second argument are the directory permissions (ignored on Windows) which default to 0777.
You also need to set $folder_full to the root using /.
$folder_full = "/images/{$getFed}/{$country_folder}/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full, 0777, true);
You need to use the recursive parameter to add directories that don't exist in the path you provide: mkdir($folder_full, 0777, true)
See the PHP docs here
All the intermediary directories have to already exist. You can trigger this behaviour by using the optional third argument:
mkdir($folder_full,0777,true);
in normal cPanel the folder permissions should be 0755
so the command in this case will be:
mkdir('tst/tst2/tst3', 0755, true);

Create writable directories

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);
?>

Is it possible to use relative paths with PHP scandir and does authentication affect scandir?

I have an authenticated directory in my site that has a bunch of directories of photos in it. If you log in you can access these photos.
/admin/galleries/
I want to build another page outside of that directory that I can grant guest access to viewing the list of directories in the authenticated directory.
/guest/access/
I just need a simple list of the directories in the /admin/galleries/ dir. I'm trying to use scandir.
$folderlist = scandir("../../admin/galleries");
This doesn't return false, but it returns empty. I'm not sure why? Is it the authentication on that directory that's blocking access via scandir? I wouldn't have thought that would have affected a server process like scandir.
Is the relative path a problem? When I make a dummy directory inside of /guest/access/ and change the scandir path to scandir("."), it outputs that directory's name. But if I move that same directory up into the /guest/ directory and change scandir to scandir("..") or scandir("../../guest"), it returns empty again. That makes me think it's not an authentication problem, but something with scandir itself?
Try to use dirname(__FILE__) before your /../../
Sorry to have left this question unanswered so long:
It wasn't returning empty, I just hadn't incorporated the correct relative path into my echo statements and had run them through an is_dir() statement that returned false because those directories didn't exist in the local directory. Dumb mistake.

How can I determine in PHP if I have write permissions to a folder

I'm writing a WordPress plugin that requires manipulating files in the plugin directory, and I can't figure out how to diagrammatically determine whether PHP has write permissions to a folder. How would I go about achieving this in a relatively simple manner?
$b = is_writable( "/file/or/folder/to/test" ); //boolean value
http://php.net/manual/en/function.is-writable.php
Works on files as well as on folders: "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."
fileperms gets permissions for the given file.
Try this function: is_writable()

Categories