mkdir() says theres no such directory and fails?
Solution 1
You have an error in your string:
mkdir("images/listing-images/rent/'.$insertID.");
should be:
mkdir("images/listing-images/rent/$insertID");
Solution 2
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.
Solution 3
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).
rpsep2
Updated on July 20, 2022Comments
-
rpsep2 almost 2 years
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!
-
zerkms over 11 yearsI'm sure his error caused not by that, but by lacking of the
images/listing-images/rent
-
zerkms over 11 yearsSo what's your final answer? There is no syntax error in the code. The string is syntactically correct (though pointless). As you said - this should be a comment :-)
-
John Conde over 11 yearsIf you'd like I can change the word
syntax
if you think that would make the answer more accurate -
zerkms over 11 yearsyour change proposed in "should be" cannot change the behaviour - the code would throw the same error
-
zerkms over 11 years"You shouldn't use is_dir()" --- any clarification for that?
-
CaffeinatedDave over 11 yearsActually disregard the first part, just checked the is_dir docs and it checks for existance as well, so it'll just be that the parent dir doesn't exist.
-
rpsep2 over 11 yearsill accept this as an answer, for helping me eliminate pointless code, although comments to my question solved my actual problem :)
-
ficuscr almost 9 yearsShould though be aware that is_dir caches results. See note in page at php.net.
-
KiX Ortillan over 8 yearsThe only one that explained with relation to rules of PHP 5.0
-
KiX Ortillan over 8 yearssame for the above one.. only 1 sec faster
-
Arvind K. about 4 yearsThis should the correct answer since there is no syntax error in the code for which the "correct" answer has been marked as correct.