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 the same methods with a form for uploading files and a database or directory-based listing and selection for downloading the file.

Here, we will use a Storage facade to store or upload files. 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 the 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 the 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, the storage name, and second the 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 the move() function to upload files. In this method, we simply chain the move() method with the request object. The move() method will take two parameters first one is a folder and the second one is a 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 files in Laravel. Here, we can also use Storage to return the file as a 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 files. The storage method will take a path as a parameter and response method will take a full file path and the second parameter is a file name.

Conclusion

This guide explores the intricacies of file uploads and downloads within Laravel, a popular PHP framework. It elucidates practical methods using the Test Controller and static file definitions. By leveraging Laravel’s Storage facade, users can seamlessly manage file operations, from uploading to downloading. The tutorial covers essential concepts, including configuring file settings, linking local storage, and employing functions like put() and download(). With clear examples and best practices, readers can adeptly handle file management in their Laravel applications.