Error creating directory with php even using file_exists

Asked

Viewed 54 times

0

Personal beauty? I have a problem which is as follows. I use the following code to create a directory.

    //verifica se existe diretorio para criar
$dir = 'imagens/'.$id_produto.'/';

if(!file_exists($dir)){
  mkdir($dir, 0777);
}else{
  echo "Erro ao criar diretório";
}

But even going there in the folder and leaving it clean, running this code it points out that the directory exists. Already displayed separately echo file_exists($dir); and it returns me 1 as if the directory existed. The strange thing is that it came to work normally and now I am with this problem. any idea?

  • Have you checked whether this relative path of var "$dir" is even pointing to where you think? What about the folder permissions?

  • I forgot that I changed this folder code and I was actually creating in another place. Thanks ai for the help.

2 answers

2

Try this

mkdir($dir, 0777, true);

  • 1

    Just specify better what your mkdir differs from your mkdir and what true changes in code operation so that the user who asked and others who see the question better understand your answer. Hug!

0

Try to use the function is_dir see in http://php.net/manual/en/function.is-dir.php

remove the last concatenation in the variable $dir, following example below.

$dir = 'imagens/'.$id_produto;
if(!is_dir($dir)){
   mkdir($dir, 0777,true);
    // verifica novamente
    if (!is_dir($dir)){
        echo "erro ao criar";
    } else{ 
        echo "dir criado agora ".time();
    }
}else{
    echo "dir existe";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.