Upload pdf file using Laravel 5

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Uploading PDF Files with Laravel 5: A Comprehensive Guide Body:

The process of uploading a user-generated file using Laravel 5 is relatively straightforward. Here's how we can create a form that allows users to upload PDF files, store them in the specified folder, and display a success message upon successful submission.

First, let us begin by defining the relevant routes and controller functions for managing the file upload process: 1. Routes: Create a route group with middleware named 'web' which will handle user requests:
Route::group(['middleware' => ['web']], function() {
    // Routes specific to the web interface
});
To upload and store PDF files, create a route for the file upload form using 'post' method:
Route::post('upload_pdf', 'FileController@store');
2. Controllers: To handle all operations related to file uploads, create a controller named FileController with the store() function:
public function store(Request $request) {
    // Code to process and store uploaded PDF files goes here.
}
Now that we have our routes and controllers set up, let's look at the view code for the form:

To store the uploaded PDF files, we need to perform the following steps: 1. Receive the file data from the request in the controller's store() function using Request::instance()->file('upload_file'):
$file = $request->file('upload_file');
2. Move the uploaded PDF to a specified upload path and folder, such as 'public/files':
$filename = time().'_'.str_random(10).'.'.$file->getClientOriginalExtension();
    $path = storage_path('app/public/files').'/'.$filename;
    $file->move($path, $filename);
3. Redirect the user back to the form or to a specific page on successful file upload:
return redirect()->route('dashboard')->with(['success' => 'PDF File Uploaded Successfully']);
4. Display relevant information about the uploaded PDF files in your view, such as filename and status:

Inside your blade file (or wherever you choose to display the file details), add the necessary HTML markup:

<div class="form-group">
     <label for="upload_file" class="control-label col-sm-3">   Uploaded Files</label>
     <div class="col-sm-9">
          <ul class="list-group">
              @foreach($files as $file)
                  <li class="list-group-item"> {{ $file->name }} ({{ $file->size }}) > ⇑
              @endforeach
          </ul>
     </div>
</div>
Remember to handle and display errors in your view as well, such as file size limitations or incorrect upload formats. You can also add additional validation rules on the controller's store() function to ensure only PDF files are uploaded. In conclusion, this tutorial demonstrates how to create a form to upload PDF files using Laravel 5 and store them in a specified folder on your server. By following these steps and best practices, you can now implement efficient and secure file upload functionalities into your application.