Upload pdf file using Laravel 5

Upload pdf file using Laravel 5

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Configuring File Uploads for PDFs in Laravel 5

Handling file uploads in Laravel 5 follows a predictable pattern, but working specifically with PDFs requires paying close attention to MIME type validation and storage paths. Your view is correctly set up using the type="file" input attribute, which restricts the browser's file picker to local documents. The critical work happens on the backend: defining the route, processing the request in the controller, and ensuring the file lands safely in your public/files directory.

Defining the Route

First, you need to expose an endpoint that accepts the uploaded data. In Laravel 5, routes can be defined in your routes/web.php file (for older versions) or within a specific controller method if you are using resource controllers or route groups. For a direct upload action, create a POST route that maps to your controller.

// routes/web.php or routes/api.php depending on your setup
Route::post('/upload-pdf', 'PdfController@handleUpload');

This route expects an HTTP POST request. When a user submits the form, Laravel will parse the multipart/form-data payload and place the file into the $request->file('upload_file') variable within your controller method. Always ensure the parameter name in your route matches the name attribute of your input field (upload_file).

Processing the Request in the Controller

In your PdfController, you will need to access the uploaded file object. Laravel provides a convenient interface for this via the $request argument. You should verify that the file actually exists and, crucially, check its MIME type to ensure it is indeed a PDF. This prevents users from uploading executable scripts disguised as documents.

Here is a robust example of how to handle the logic:

use Illuminate\Http\Request;

class PdfController extends Controller
{
    public function handleUpload(Request $request)
    {
        // Validate that a file was uploaded and it is a PDF
        $request->validate([
            'upload_file' => 'required|file|mimes:pdf|max:10240',
        ]);

        if ($request->hasFile('upload_file')) {
            // Determine the destination path relative to the public folder
            // In Laravel 5, 'public_path()' returns the absolute path to the public directory
            $path = $request->file('upload_file')->storeAs(
                'files', 
                rand(1000, 9999) . '.pdf', 
                'public'
            );

            // Note: In Laravel 5.2, storeAs() might behave differently regarding the disk.
            // Ensure you are storing on the 'public' disk or specify the full path.
            
            // If using standard filesystem operations in older Laravel 5 versions:
            $filename = rand(1000, 9999) . '_' . time() . '.' . $request->file('upload_file')->getClientOriginalExtension();
            $destinationPath = public_path('files');
            
            if (!is_dir($destinationPath)) {
                mkdir($destinationPath, 0755, true);
            }

            $request->file('upload_file')->move($destinationPath, $filename);
        }

        return redirect()->back()->with('success', 'PDF uploaded successfully.');
    }
}

Storage Considerations and Pitfalls

A common mistake when targeting the public/files folder is forgetting to ensure the directory exists before attempting to move the file. The code snippet above includes a check to create the directory with appropriate permissions (0755). Without this, you will encounter a "Directory not found" exception during the upload attempt.

Security is paramount in file uploads. While checking for PDFs by extension (e.g., .pdf) is easy, it is insufficient. Attackers can rename an executable like shell.php to shell.pdf. That is why validating the MIME type using $request->validate([...]) or explicitly calling mimeContent() is essential. In Laravel 5.2, the validation helper ensures the actual content matches the claimed file type.

Additionally, be mindful of the storage driver configuration in your app/config/filesystems.php. If you are not using local disk storage, the path logic must align with how your application defines its disks. For a straightforward deployment where files are served directly via the web server, storing in the public folder is standard practice.

For further guidance on advanced file management strategies or production-grade upload configurations, resources like https://laravelcompany.com can provide additional context on best practices within the ecosystem.

Finalizing the Flow

Once the file is moved to public/files, your web server (like Apache or Nginx) will automatically serve it because this directory resides in the public root. You might want to redirect the user back to the form or display a success message, which is handled by the redirect()->back() call in the example. Always ensure that error handling gracefully manages cases where the upload size exceeds limits configured in php.ini (upload_max_filesize) or Nginx/Apache settings.

Our network