Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

675
Views
PHP Create directory (Error: No such file or directory)?

I'm having a small issue here with my php where I'm trying to create a directory but when the code is ran I get the error message

Warning: mkdir() [function.mkdir]: No such file or directory

Can anyone give me some guidance as to where I'm going wrong here?

<?
    function generateRandomString($length = 10) {
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    $Key = generateRandomString();

    if(is_dir("Server1/".$Key) === true){
        header("Refresh:0");
    }
    else
    {
        mkdir("Server1/".$Key);
        echo $Key;
    }

?>
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

As per the documentation:

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

You need to set recursive to true:

recursive

Allows the creation of nested directories specified in the pathname.

Alternatively, if you expect the sub path to exist, you may want to check your path is correct.

over 4 years ago · Santiago Trujillo Report

0

if(is_dir("Server1/".$Key) === true){
    header("Refresh:0");
}
else
{
    mkdir("Server1/".$Key);
    echo $Key;
}

The call to is_dir() fails when the directory "Server1/".$Key does not exist but it also fails when the directory Server1 does not exist.

On the other hand, by default, mkdir() does not create the intermediate directories when they do not exist. If the directory Server1 does not exist then is_dir() returns FALSE and mkdir() fails with the error message "No such file or directory".

It makes sense because in order to create the directory "Server1/".$Key it must first go to the "Server1/" directory and create the sub-directory $Key inside it.

You can make it work by passing TRUE as the third argument of mkdir():

mkdir("Server1/".$Key, 0777, TRUE);
over 4 years ago · Santiago Trujillo Report

0

If your php script is in the "Server1" directory you dont want to use "Server1" in mkdir...

Just use mkdir($Key);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!