mkdir(): No such file or directory - Laravel

Asked

Viewed 992 times

0

I have a problem in Laravel, when creating a folder in the Windows, with this method it creates the folder in the Linux, displays the error:

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

My saved function 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_arquivos\2018\162 only to be displaying the message.

  • What is the line line 435?

2 answers

2


First, I believe that File::makeDirectory is laravel4 and not 5, paragraph 5 I believe that this function does not even exist (if I am not mistaken)

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

C:\xamp2\htdocs\pmi\public\projetos_arquivos\2018\162

So if that’s all you have to do is create recursively (itself makeDirectory does it) this way:

// 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 so:

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

Now if it really is Laravel 5, I think you’re actually using Illuminate\Filesystem::makeDirectory, the principle 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);
  • 1

    Thank you William , thank you very much =] that was the example that worked was the example quoted in the 4 placed and managed to create the folder 2018 and put the pdf file inside it =] perfect !!!

1

How you are using a server Linux, need to give permissions, the native user of php which makes the creation of folders is www-data.

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

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

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

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

I hope it helps you!

  • my friend .. Excuse me in linux he creates... it is in windows that does not create the folder ,I will edit Putz!!!

  • Windows doesn’t have many such restrictions, maybe you didn’t point to the directory correctly, so it’s saying it doesn’t find the location. Take it easy.

Browser other questions tagged

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