Laravel File Upload Tutorial

INTRODUCTION
Learn easy way to upload files either to FTP or two cloud storage like Amazon S3 using Laravel framework's inbuilt Filesystem
ABOUT THE AUTHOR
Kaustubh has been developing with Laravel framework for a few years now and is committed to helping newbie PHP developers get started with Laravel development.
Kaustubh Katdare
Kaustubh Katdare
CEO @ CrazyEngineers, TEDx Speaker, Guitarist

Uploading files with Laravel continues to be one of the 'difficult' tasks for several newbie programmers. In this short tutorial, I'll explain how easy and straightforward it is to upload any file to any storage system of your choice with Laravel.

Learn to Upload Files with Laravel

I'll assume that you have installed Laravel on your local development machine and are ready to understand how file uploading works. We'll try to upload a file to our FTP server and Amazon S3 Storage. 

Laravel's own filesystem is based on the popular Flysystem PHP package; which makes it tad easy to configure our 'Disks' aka storage locations. 

DqD1-laravel-file-upload.jpg

Let's Configure FTP Driver

You might already know that in Laravel, there are only two locations to look for - the .env file and the appropriate configuration file in 'config' directory; which in our case is 'config/filesystems.php'.

Open the file and make sure that it has the following -

'ftp'    =>    [
    'driver'    => 'ftp',
    'host'      => 'ftp.your-domain.com',
    'username'  => 'ftp-username',
    'password'  => 'ftp-password'
],    

Note: If you want to use SFTP (which you should) make sure that you have installed the SFTP composer package: league/flysystem-sftp ~1.0

Note that we are referring to our FTP configuration as 'ftp' aka 'FTP Disk'. There's a 'Storage' facade that we'll use to access our Disk instances. 

Access the FTP Disk 

Accessing FTP disk, or any configured 'disk' in Laravel is easy. We do it with our Storage Facade. Look at the simple one line code below -

Storage::disk('ftp')->put('directory/filename', $file_contents);

This line is super important. We are instructing Laravel's storage system to make use of the 'ftp' disk that we just configured. The next instruction is to provide the complete location (including filename with extension) and with the content to be stored in the file. 

When I was getting started, I made the mistake of not providing filename with extension as the first parameter and kept wondering why the file wasn't getting uploaded to the disk. Don't repeat the mistakes I did.

Note: You might wonder what should you provide when the file. Let's look at it a bit in more detail -

// Controller
class FileUploadController extends Controller {
    public function store(Request $request) {
        $uploaded_file    =    $request->file('file');
        $file_name        =    $uploaded_file->getClientOriginalName();
        Storage::disk('ftp')->put('path/to/file.txt', $uploaded_file);
    }
}

Those three lines will put your file to appropriate location on your FTP server. Note that I did not include the front-end code to handle file upload. Let me know in the comments if you'd like HTML code as well.

Configuring Amazon S3 with Laravel Storage

In order to use Amazon S3, install composer package: league/flysystem-aws-s3-v3 ~1.0

Once the package is installed, you are now ready to configure your S3 disk. In your 'config/filesystems.php' file, add the following configuration -

's3'    =>    [
    'driver'    =>    's3',
    'key'       =>    env('AWS_ACCESS_KEY_ID'),
    'secret'    =>    env('AWS_SECRET_ACCESS_KEY'),
    'region'    =>    env('AWS_DEFAULT_REGION'),
    'bucket'    =>    env('AWS_BUCKET'),
    'url'       =>    env('AWS_URL'),
],

That's it. You can now store your files to S3. All you need to do is simply change the disk when using Storage facade. Do it like this -

Storage::disk('s3')->get(file.text); // Access S3 Disk

Configure and Use Multiple Disks

It's a no brainer that you can now configure and use multiple disks in Laravel. Let's say you wish to upload a file and process it on your server before sending it to the cloud disk, you may do so by first storing it in the local or 'FTP' disk; and then move it to the cloud using Cloud Disk. 

I've only touched upon the very basic of the code required to use Filesystem in Laravel framework. I'm willing to expand it to address your needs. Let me know if this tutorial was helpful and make suggestions. I'll keep it updated.

Happy Laraveling!

Share this content on your social channels -

Replies, Feedback and Questions

Radhika Deshpande
Radhika Deshpande
Computer Science

06 Jan, 2019

Informative content.

Mohit G
Mohit G
Branch Unspecified

07 Jan, 2019

File uploads in Laravel are quite easy with the approach you have advised. Otherwise, handling on our own is quite a pain. Perhaps you should update your tutorial with uploaded file validation mechanism. It's essential to validate every file that is being stored on the server.

I'm going to experiment with dynamically setting the Disk based on the kind of file being uploaded to Laravel. That is, if it's an image file - send to to Disk1; if it's a PDF, send it to Disk2 and likewise. It's my own, strange requirement though. Great tutorial. Looking forward to more. 

Mohit Patil
Mohit Patil
Software Developer and an Aspiring Entrepreneur

11 Jan, 2019

Can you recommend any library that will automatically compress the uploaded images? My goal is to compress the images while keeping their resolution before saving them to cloud disk or SFTP. Thank you for the tutorial. 

Kaustubh Katdare
Kaustubh Katdare
CEO @ CrazyEngineers, TEDx Speaker, Guitarist

11 Jan, 2019

Mohit - Image Intervention and Image by Spatie are the two composer packages you can deploy in your application to achieve the desired functionality. Image intervention will allow image conversion and cropping along with all other goodies. For compression, look at the package by Spatie. Very good packages and image compression results are quite awesome.