mkdir() says theres no such directory and fails?

PhpUploadMkdir

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

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 2 - Php

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

Solution 3 - Php

You have an error in your string:

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

should be:

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

Solution 4 - Php

  • recursive Allows the creation of nested directories specified in the path name.
  • but did not work for me!! for that here is what i came up with!!
  • and it work very perfect!!

> $upPath = "../uploads/RS/2014/BOI/002"; // full path
$tags = explode('/' ,$upPath); // explode the full path
$mkDir = ""; > > foreach($tags as $folder) {
> $mkDir = $mkDir . $folder ."/"; // make one directory join one other for the nest directory to make > echo '"'.$mkDir.'"
'; // this will show the directory created each time > if(!is_dir($mkDir)) { // check if directory exist or not > mkdir($mkDir, 0777); // if not exist then make the directory > } > }

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

$path = 'd:\path\to\my\file';
mkdir($path, null, true);

This is copied from the php manual. The last argument "true" allows the creation of subfolders

Solution 8 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrpsep2View Question on Stackoverflow
Solution 1 - PhpzerkmsView Answer on Stackoverflow
Solution 2 - PhpadamdunsonView Answer on Stackoverflow
Solution 3 - PhpJohn CondeView Answer on Stackoverflow
Solution 4 - PhpRamyzView Answer on Stackoverflow
Solution 5 - Phpuser3410311View Answer on Stackoverflow
Solution 6 - PhpLuca AntonelliView Answer on Stackoverflow
Solution 7 - PhpDiogene KalisaView Answer on Stackoverflow
Solution 8 - PhpCaffeinatedDaveView Answer on Stackoverflow