mkdir() says theres no such directory and fails?

94,290

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).

Share:
94,290
rpsep2
Author by

rpsep2

Updated on July 20, 2022

Comments

  • rpsep2
    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
    zerkms over 11 years
    I'm sure his error caused not by that, but by lacking of the images/listing-images/rent
  • zerkms
    zerkms over 11 years
    So 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
    John Conde over 11 years
    If you'd like I can change the word syntax if you think that would make the answer more accurate
  • zerkms
    zerkms over 11 years
    your change proposed in "should be" cannot change the behaviour - the code would throw the same error
  • zerkms
    zerkms over 11 years
    "You shouldn't use is_dir()" --- any clarification for that?
  • CaffeinatedDave
    CaffeinatedDave over 11 years
    Actually 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
    rpsep2 over 11 years
    ill accept this as an answer, for helping me eliminate pointless code, although comments to my question solved my actual problem :)
  • ficuscr
    ficuscr almost 9 years
    Should though be aware that is_dir caches results. See note in page at php.net.
  • KiX Ortillan
    KiX Ortillan over 8 years
    The only one that explained with relation to rules of PHP 5.0
  • KiX Ortillan
    KiX Ortillan over 8 years
    same for the above one.. only 1 sec faster
  • Arvind K.
    Arvind K. about 4 years
    This should the correct answer since there is no syntax error in the code for which the "correct" answer has been marked as correct.