mkdir (): No such file or directory - Laravel

0

I have a problem with Laravel , referring to the time to create a folder in Windows , with this method it creates the folder in Linux , it displays the error:

  

ErrorException in Filesystem.php line 435:mkdir(): No such file or directory

My role that saves on Linux:

public function salvar($objArquivo, $objProjeto, $objDataAtualizacao) 
{
    if (is_object($objProjeto->data_criacao)) 
    {
        $dataAbertura = $objProjeto->data_criacao->format('Y');
    } 
    else 
    {
        try 
        {
            $anoAberturaProjeto = 
                \DateTime::createFromFormat('Y-m-d H:i:s.u', 
                                $objProjeto->data_criacao);
        } 
        catch(\Exception $e) 
        {
            $anoAberturaProjeto = 
                \DateTime::createFromFormat('Y-m-d H:i:s', 
                                $objProjeto->data_criacao);
        }
        $dataAbertura = $anoAberturaProjeto->format('Y');
    }
    // 'public/projetos_arquivos/year/codigo_projeto'
    $strCaminho = public_path('projetos_arquivos'). 
                    DIRECTORY_SEPARATOR . $dataAbertura . 
                    DIRECTORY_SEPARATOR . $objProjeto->codigo; 

    // Nomeia arquivo com codigo do projeto + data passada como argumento
    $strNome = $objProjeto->codigo . "_" . $objDataAtualizacao->format("Y_m_d"); 

    // Cria pasta para o projeto, caso não já exista uma   
    if(!file_exists($strCaminho)) 
    {          
        var_dump($strCaminho);
        $objProjetoDiretorio = File::makeDirectory($strCaminho);
    }

    // Salvando arquivo no servidor
    $objArquivo->move($strCaminho, $strNome . ".pdf"); 
    $strCaminhoArquivo = $strCaminho . DIRECTORY_SEPARATOR . $strNome . ".pdf";

    return($strCaminhoArquivo);
}

I even put a var_dump($strCaminho); and the path displays like this C:\xamp2\htdocs\pmi\public\projetos_arquivos182 only to be displaying the message.

    
asked by anonymous 19.03.2018 / 17:55

2 answers

2

First, I think that File::makeDirectory is laravel4 and not 5, in 5 I do not even think this function exists (if I'm not mistaken)

Maybe you are trying to create a folder that does not exist, for example, there is no 2018 , so you can not create the 162 folder in the path:

C:\xamp2\htdocs\pmi\public\projetos_arquivos182

So if this is enough you just create it recursively (own makeDirectory does this) like this:

// Cria pasta para o projeto, caso não já exista uma   
if(!file_exists($strCaminho)) 
{
    $objProjetoDiretorio = File::makeDirectory($strCaminho, 0775, true);
}

To suppress the errors would use like this:

$objProjetoDiretorio = File::makeDirectory($strCaminho, 0775, true, true);

Now if it really is Laravel 5, I think you're actually using Illuminate\Filesystem::makeDirectory , the beginning is the same as File :

use Illuminate\Filesystem;

...

// Cria pasta para o projeto, caso não já exista uma
if(!file_exists($strCaminho)) 
{
    $objProjetoDiretorio = Filesystem::makeDirectory($strCaminho, 0775, true);
}

If you want to suppress errors:

$objProjetoDiretorio = Filesystem::makeDirectory($strCaminho, 0775, true, true);
    
19.03.2018 / 21:52
1

Since you are using a Linux server, you need to give permissions, the native user of php that makes folder creation is www-data .

So in the folder for example projetos_arquivos , you need to change the owner of it to the user www-data of php so that it has write permission and modification.

Do the following, generate this command in the root where is the fixed folder that I imagine to be projetos_arquivos :

chown -R www-data:www-data ./projetos_arquivos

This allows a new owner, its folder where it will contain other subfolders may contain others dynamically created now.

I hope it helps you!

    
19.03.2018 / 18:05