Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir() says theres no such directory and fails?

Tags:

php

mkdir

upload

Im likely doing something very simply wrong, but when I try to make a directory (using a variable of an insert just performed as the last folder name), I get the error:

Warning: mkdir() [function.mkdir]: No such file or directory in /home/blah/blah

with the code:

if (!is_dir("images/listing-images/rent/'.$insertID.")) {         //make new directory with unique id    mkdir("images/listing-images/rent/'.$insertID.");  } 

of course the directory doesn't exist.. I'm trying to make it now? confused!

like image 536
rpsep2 Avatar asked Feb 21 '13 21:02

rpsep2


People also ask

Why does mkdir not create directory?

If you get a permission denied error, you have not permissions to create a directory in the specified path. Check if you can get around the problem by modifying the group membership or ownership, so that you get the permission needed for the whole directory path involved.

Can mkdir fail?

The mkdir() function shall fail if: [EACCES] Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be created.


2 Answers

It happens because you don't have images/listing-images/rent path existing in your filesystem.

If you want to create the whole path - just pass the 3rd argument as a true:

mkdir('images/listing-images/rent/'.$insertID, 0777, true); 

There is also a chance you're in a wrong directory currently. If this is the case - you need to change the current dir with chdir() or specify the full path.

like image 198
zerkms Avatar answered Sep 16 '22 11:09

zerkms


Assuming you're using PHP > 5.0.0, try mkdir("path", 0777, true); to enable creating directories recursively (see here: http://php.net/manual/en/function.mkdir.php).

like image 43
adamdunson Avatar answered Sep 18 '22 11:09

adamdunson