Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

682
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda