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

3

I have a form to upload files, I have input of type file :

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

Then, if a file has been selected, I want to create a " PDF's " folder. If the folder already exists I do not create, but if it does not exist, I create it dynamically and later I 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 an error with this code:

  

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

Why might this error be happening?

    
asked by anonymous 17.05.2014 / 17:33

1 answer

7

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

In PHP 5.0.0 a third argument was added to change this behavior: recursive . The default value is false . So just change it to that:

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

Documentation: mkdir

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

    
17.05.2014 / 17:50