How to download or upload files in laravel

Nowadays, File upload and download functionality is almost mandatory. Laravel provides simple and easy ways to upload and download files. You can store these files in a public folder or local storage.

In this blog, we will see some practical examples to upload and download files in Laravel. Here, we have used Test Controller and statically defining files. You can use same methods with a form for uploading files and database or directory-based listing and selection for downloading the file.

Here, we will use a Storage facade to store or upload file. Before using upload and download functionality make sure file configurations for your application are. Sometimes storing files in a custom location or in a custom directory structure you need to configure it in filesystems.php.

Before starting, link your local storage with an application using below command:

php artisan storage:link

Uploading and Storing Files in Laravel

In Laravel, there are many ways to store or upload files into your application even some libraries are also available to handle file storage. While file uploading Laravel gives a unique name to file but you can change it as per your requirements.

The storage facade provides many functions to store files. But put() and putFileAs() methods are commonly used. The put() function will take two parameters first one is the storage name and second one is file. Let's take an example to understand both functions.

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index(){
        $file1 = Storage::put('images', $request->file('image'));

        $name = $request->file('image')->getClientOriginalName();
        $file2 = Storage::putFileAs('images', $request->file('image'), $name);

        dd($file1, $file2);
    }
}

Output :

images/test1.jpg
images/test2.jpg

Here, we can also use move() function to upload file. In this method, we simply chain move() method with the request object. The move() method will take two parameters first one is a folder and the second one is file name.

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index(){
        $name = $request->file('image')->getClientOriginalName();
        $file = $request->file('image')->move('images', $name);

        dd($file); // test.jpg
    }
}

Downloading File in Laravel

Downloading files is much easier than uploading in Laravel. The storage facade's download method is used to download file in Laravel. Here, we can also use Storage to return file as response with some header if required.

The download() method takes the file path as a parameter. It will automatically start downloading. It's considered best practice to check file exists or not before downloading. Let's take an example to download a file on Laravel:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function index(){
        return Storage::download('images/test.jpg');

        //or

        return \Response::download(public_path(). "/images/test.jpg", 'test.jpg', ['Content-Type: image/jpeg']);
    }
}

In the above example, we have used two methods to download file. The storage method will take a path as a parameter and response method will take a full file path and second parameter is file name.

Conclusion

In this article, you have learned to upload and download files using multiple methods in Laravel.