morteza_saberi's avatar

How to upload pdf file in laravel

Hi how can I upload pdf file in laravel?

0 likes
11 replies
Snapey's avatar

There are loads of upload tutorials available. PDF is no different to any other type of upload.

Snapey's avatar

'don't work'

Can't help you there I'm afraid. Try googling 'don't work' - see what you get

morteza_saberi's avatar

if($request->hasFile('file')){

        $filenameWithExt = $request->file('file')->getClientOriginalName();

        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

        $extension = $request->file('file')->getClientOriginalExtension();

        $fileNameToStore = $filename.'-'.time().'.'.$extension;

        $path = $request->file('file')->storeAs('public/file', $fileNameToStore);

     } else {
        $fileNameToStore = 'noimage.jpg';
     }

     //  Create 
     $monogeraf = new Monogeraf;
     $monogeraf->thesis_code = $request->input('thesis_code');
     $monogeraf->student_code = $request->input('student_code');
     $monogeraf->title = $request->input('title');
     $monogeraf->author = $request->input('author');
     $monogeraf->supervisor = $request->input('supervisor');
     $monogeraf->advisor = $request->input('advisor');
     $monogeraf->department = $request->input('department');
     $monogeraf->date_defance = $request->input('date_defance');
     $monogeraf->file = $fileNameToStore;
     $monogeraf->save();

         if($monogeraf){
        return redirect()->route('monogerafs.index', ['monogeraf'=>$monogeraf->id])
                         ->with('success', 'Monogeraf Create successfully');
    }
     return back()->withInput();

this is my source.

Snapey's avatar

How far does it get through your code?

What errors do you see?

Do you end up with 'noimage.jpg' written the the database?

Snapey's avatar

so request does not have 'file'

We would need to see the form to help there

Snapey's avatar

you can examine the request object dd($request) and see if it contains your file, and you can post your upload form here

edoc's avatar

Do you have enctype="multipart/form-data" on your form element?

<form action="/your-url" method="post" enctype="multipart/form-data">
</form>

@morteza_saberi

Please or to participate in this conversation.