"Path cannot be empty" laravel

Viewed 2809

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');
    }
3 Answers

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

You can try one of these method

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

I just solved this problem. If your website is hosted on windows server go to your temp folder (C:\Windows\Temp) and right click -> properties -> edit your user's permissions to full control like this.

go to properties

edit permissions

select your user and check 'full control'

finally apply

after that if you get an alert just click ok. Don't forget to login with your administrator user to able to edit permissions.

Related