Error "No such file or directory" when using mkdir()

Asked

Viewed 4,251 times

3

I have a file upload form, so I have one input of the kind file:

<input type="file" class="fileinput" name="pdf"/>

Then, if a file has been selected, I want to create a folder "PDF’s". If the folder already exists I do not create, but if it does not exist I create dynamically and then upload:

if(!empty($_FILES['pdf']['tmp_name'])){                 
    $pasta  = '../pdfs/';   
    $ano    = date('Y');
    $mes    = date('m');

    if(!file_exists($pasta.$ano)){
    mkdir($pasta.$ano,0755);                    
}

But I’m having a mistake with this code:

Warning: mkdir(): No such file or directory in mkdir($pasta. $ano,0755);

Why could this mistake be happening?

1 answer

8


The point is that the mkdir creates only a directory, not a sequence of them. For example, if you want to create the path arquivos/anual/2014/pdf/, the briefcase arquivos/anual/2014/ must already exist in advance.

In PHP 5.0.0 a third argument was added to change this behavior: recursive. Its default value is false. Then you just change into this:

mkdir($pasta.$ano, 0755, true);

Documentation: mkdir


If this is not the problem, it may be the permission of the parent directory. Perhaps the user cannot create the folder there. Check this.

  • Thank you so much for the solution and the explanation! That’s the problem, I tried to create the folder first, and I stopped having the error!

Browser other questions tagged

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