How to Force Download Files Using Laravel 5.7

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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.

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

Read Also:  Add Word Count to Single Posts in WordPress

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:  Different Types of SQL Keys in Database with Example

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.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *