10

I want to save a pdf file in the storage folder but when I insert a file in my form and I click on the button, it displays me the error "Path cannot be empty". However, the path is not empty. Here is my function that allows me to do this :

public function getFilenametostore(Request $request): string
{
    $filenamewithextension = $request->file('profile_pdf')->getClientOriginalName();

    $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

    //get file extension
    $extension = $request->file('profile_pdf')->getClientOriginalExtension();
    $time = time();
    //filename to store
    $filenametostore = $filename . '_' . $time . '.' . $extension;

    //The problem is here
    $request->file('profile_pdf')->storeAs('profile_pdfs', $filenametostore);
    

    return $filenametostore;
}

in storage, there is: storage/app/public/profile_pdfs/

My Form :

<form action="{{url('poste')}}" method="post" enctype="multipart/form-data">
@csrf

    <div class="mt-2">
        <label class=" block text-sm text-gray-600" for="cus_email">Upload</label>
        <input class="px-2 py-2 text-gray-700 rounded" type="file" name="profile_pdf" id="exampleInputFile">
    </div>


    <div class="mt-4">
      <button class="px-4 py-1 text-white font-light tracking-wider bg-gray-900 rounded" type="submit">Publier</button>
    </div>


</form>

My controller :

if($files = $request->hasFile('profile_pdf'))
    {

        $filenametostore = $this->getFilenametostore($request);


        $p = new Poste;
        $p->pdf = $filenametostore;
        $p->save();

        return redirect()->route('poste.index');
    }
2
  • Is it this function that throws the error? or when you save your model? I guess you've added the 'path' as required in your validation? Add the controller code that calls this function and saves the model. Commented Nov 24, 2021 at 10:05
  • I found the problem, it is because of the putFileAs() function in the vendor with $file->getRealPath() Commented Nov 25, 2021 at 12:47

4 Answers 4

16

Go to php.ini file uncomment upload_tmp_dir and set the value c:\windows\temp

;upload_tmp_dir =

Replace like this upload_tmp_dir = c:\windows\temp if you use windows

What i wanna add to this is that, when also copied "c:\windows\temp", the issue still persist thne i later got to know the file path for the temp folder is different, like mine, am using laragon, so i have to go to laragon and locate the tmp folder inside laragon then copied it like this:

upload_tmp_dir = C:\laragon\tmp

So, yours may also be different from both of us so just understand the logic behind the path and not just copy blindfoldedly.

Sign up to request clarification or add additional context in comments.

1 Comment

In case you are using Laragon, Uncomment upload_tmp_dir and set value to "C:/laragon/tmp" inside php.ini
14

If your website is hosted on Windows, edit the properties of your temp folder (typically C:\Windows\Temp.) Select the security tab and edit permissions of the web server user (or the Users group) to allow full control.

select security tab, click edit button select your user or group, allow 'full control'

Don't forget to login with your administrator account to be able to edit permissions.

Comments

2

You can try one of these method

$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or 
Storage::disk('local')->put($path . '/' . $fileName , $request->file);

Comments

1

When uploading large files, for some reason the Path gets set to empty in the Laravel Illuminate\Http\UploadedFile class, unless your php.ini upload_max_filesize and post_max_size are set appropriately.

Find the loaded php.ini (in my case the correct path is /etc/php/8.2/fpm/php.ini) — you can only find the REAL loaded file by checking phpinfo() — the php --ini command sometimes does not show the proper path.

In my case I had to allow users to upload huge SQL dump files, so I set the values like this:

upload_max_filesize = 100M

post_max_size = 100M

NOTE: If you are running nginx on your web server, you have to add client_max_body_size 100M; into your nginx config, in the server block, otherwise your users will get ugly 413 Request Entity Too Large errors when uploading (SEE: 413 Request Entity Too Large - File Upload Issue)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.