mkdir() says theres no such directory and fails?

后端 未结 7 2033
逝去的感伤
逝去的感伤 2020-12-16 09:07

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:

相关标签:
7条回答
  • 2020-12-16 09:30

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

    0 讨论(0)
  • 2020-12-16 09:41

    You have an error in your string:

    mkdir("images/listing-images/rent/'.$insertID.");
    

    should be:

    mkdir("images/listing-images/rent/$insertID");
    
    0 讨论(0)
  • 2020-12-16 09:41

    in my case $insertID was generated from some data as string by concatinating

    $insertID=$year.$otherId;
    

    I simple rewrote code like this and error disappeared:

    $insertID=(int)($year.$otherId);
    
    0 讨论(0)
  • 2020-12-16 09:45

    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.

    0 讨论(0)
  • 2020-12-16 09:48

    Probably the real error was that he forgot an extra apex.

    This:

    mkdir("images/listing-images/rent/'.$insertID.");
    

    Inside:

    /'.$insertID."
    

    Correct Version:

    /".$insertID
    

    Extended Correct Version:

    mkdir("images/listing-images/rent/".$insertID);
    
    0 讨论(0)
  • 2020-12-16 09:49

    You shouldn't use is_dir() to check if something exists, you want file_exists() as well. Try:

    if (file_exists("images/listing-images/rent/$insertID") {
        mkdir("images/listing-images/rent/$insertID.");
    }
    

    Have taken the '. out since it looks like a syntax error, but you might have a legitimate reason to keep it in.

    If the mkdir still fails, it could be that images/listing-images/rent doesn't exist, you'll have to create that separately if so.

    0 讨论(0)
提交回复
热议问题