DEV Community

Cover image for How To Download File in Laravel 8
Code And Deploy
Code And Deploy

Posted on • Edited on

10 2

How To Download File in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-download-file-in-laravel-8

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

In this short post, I'm sharing how to download files in Laravel 8. If have a project like an online digital shop in which users can download files after they purchase then this is for you. We are using a download() function from the Laravel 8 Response class to cater the download.

$filepath = a path for target file to be download

$filename = filename for the downloaded file

$headers = this is an array about the file content type to download

Response::download($filepath, $filename, $headers)

Now you have a basic idea about this function.

We will try to create a route for this.



Route::get('/purchased-download', [\App\Http\Controllers\PurchasedFileController::class, 'download'])->name('purchased.download');



Enter fullscreen mode Exit fullscreen mode

Then let's write our controller, see the complete code below:



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class PurchasedFileController extends Controller
{
    public function index()
    {
        $path = public_path('for_pro_members.zip');
        $fileName = 'purchase_files.zip';

        return Response::download($path, $fileName, ['Content-Type: application/zip']);
    }
}


Enter fullscreen mode Exit fullscreen mode

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-download-file-in-laravel-8 if you want to download this code.

Happy coding :)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
jurajchovan profile image
Juraj Chovan

hi friend,
if I want download some file, then delete it, I can use command:
return response()->download($SomeFileName)->deleteFileAfterSend(true);
but open question is:
if I want download some file, then delete it and then redirect Laravel application to some other page/route?
What is code for this case? Can you help me pls ...

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more