How to Force Download Files Using Laravel 5.7

Today, We want to share with you How to Force Download Files Using Laravel 5.7.In this post we will show you Laravel 5.6 Force Download a pdf File, hear for Laravel 5.7 – Force Download File using response helper we will give you demo and example for implement.In this post, we will learn about Laravel 5.7 force download file with the response helper method with an example.

How to Force Download Files Using Laravel 5.7

There are the Following The simple About How to Force Download Files Using Laravel 5.7 Full Information With Example and source code.

Read Also:  Vue.js Back to Top Component (vue smooth scroll)

As I will cover this Post with live Working example to develop downloading pdf file using html code, so the Force File Download With Laravel for this example is following below.

Example 1:Force File Download With Laravel

HTML Blade View Files

<a href="{{ url('') }}/download/{{$attach->file_name}}" class="btn btn-primary btn-sm btn-raised doc-download" title="{{$your_attach_object->file_name}}"><i class="fa fa-download"></i> Download</a> </p>

Laravel Define Routes

project-name-system/routes/web.php

Route::get('/download/{file}', '[email protected]');

Laravel Create a Controller

app/Http/Controllers/HomeController.php

public function download($file_name) {
    $file_path_full =base_path()."/img/atteach/".$file_name;
    $file_path =pathinfo(base_path()."/img/atteach/".$file_name);

    $basename = $file_path['basename'];
    $path = $file_path['dirname'];
    return response()->download($file_path_full, $basename, ['Content-Type' => 'application/force-download']);
  }

Example 2: Forcing File Downloads in PHP Laravel

  return response()->download($file, "product-2019 - invoice.pdf");

  or

  Route::post('downloads', function (Request $request) {
    //Laravel cache the file
    $file = $request->file('photo');

    //Laravel create a new filename. getClientOriginalExtension() for the (png, jpg) file extension
    $get_filename = 'User-photo-Profiles' . time() . '.' . $file->getClientOriginalExtension();

    //Laravel save/store to storage/app/photos as the new $get_filename
    $storage_path = $file->storeAs('photos', $get_filename);

    dd($storage_path);
});

Example 3: Laravel 5.5 – Force Download File using response helper

Laravel define Route

routes/web.php

Route::get('fileforceDonwload', '[email protected]');

Add Controller Method

app/Http/Controllers/HomeController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
    public function fileforceDonwload()
    {
        $pathTo_orignalFile = public_path("product_invoice_2020.pdf");
        $rename_file_name = time().'.pdf';
        $headers = ['Content-Type: application/pdf'];
        return response()->download($pathTo_orignalFile, $rename_file_name, $headers);
    }

}

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  how to get last insert id in php?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to Force Download Files Using Laravel 5.7.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Form Validations using Angular Example
  2. Read Also:  How to Retrieve Domain Name from URL in PHP?
  3. Read Also:  Check if Object is empty in Laravel 6 Examples
  4. Read Also:  Add Remove multiple Input Fields Dynamically in jQuery
  5. Read Also:  PHP Force File Download HTML button code
  6. Read Also:  Laravel 6 Eloquent Query Retrieving Single Models
  7. Read Also:  UI Grid Filtering using Angular Example
  8. Read Also:  Real time progress bar With PHP
  9. Read Also:  $event Object as Parameter using Angular Example
  10. Read Also:  how to get last insert id in php?
CLOSEX