How to Upload Images to Another Server Through FTP in Laravel

Do you want to upload your files from your Laravel application to another server through FTP? Laravel comes with the built-in support for uploading files to external server or cloud. In this guide, we show you how to upload files(images) to another server through FTP in Laravel website.

Hosting images on another server can help to improve your site speed. This technique is kind of parallel connection between a web browser and the servers. It reduces the load from one server as for images browser will give a call to another server.

Laravel Filesystem provides a convenient way to manage our files on the cloud or external server. The user can choose different Laravel storage providers like S3, Rackspace, FTP or SFTP. Let’s take a look at Laravel file upload using FTP driver.

Upload Files to Another Server Through FTP Driver

To get started, you should know the details of your FTP host, FTP username, and FTP password. Once, you are ready with the details open the .env files and add the details as below:

FTP_HOST=YOUR_FTP_HOST_VALUE
FTP_USERNAME=YOUR_FTP_USERNAME_VALUE
FTP_PASSWORD=YOUR_FTP_PASSWORD_VALUE

Make sure to replace the placeholders with the actual values. Next, open the config/filesystems.php file and add the ‘ftp’ configuration to the ‘disks’ array.

config/filesystems.php

<?php
return [
    ......
    'disks' => [
        ......
        'ftp' => [
            'driver' => 'ftp',
            'host' => env('FTP_HOST'),
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
            'root' => 'DIR_PATH_TO_WHERE_IMAGE_STORE' // for example: /var/www/html/dev/images
        ],
    ],
    
];

Replace DIR_PATH_TO_WHERE_IMAGE_STORE with the actual path where you need to store images. For example, if we have folder called ‘images’ and path to this folder is /var/www/html/dev/images then this path will go as the value for ‘root’ in the above array.

For uploading files through FTP we need a form where a user can submit the image. Add the below code in your view file.

<form action="{{ url('PASS_ACTION_URL_HERE') }}" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="exampleInputFile">File input</label>
        <input type="file" name="profile_image" id="exampleInputFile" multiple />
    </div>
    {{ csrf_field() }}
    <button type="submit" class="btn btn-default">Submit</button>
</form>

You should replace the PASS_ACTION_URL_HERE with your actual route. As we are using a Laravel Storage to file uploading user need add Facade to the controller file as follows:

use Illuminate\Support\Facades\Storage;

Finally, your controller function code would be as below:

public function store(Request $request)
{
    if($request->hasFile('profile_image')) {
        
        //get filename with extension
        $filenamewithextension = $request->file('profile_image')->getClientOriginalName();

        //get filename without extension
        $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

        //get file extension
        $extension = $request->file('profile_image')->getClientOriginalExtension();

        //filename to store
        $filenametostore = $filename.'_'.uniqid().'.'.$extension;

        //Upload File to external server
        Storage::disk('ftp')->put($filenametostore, fopen($request->file('profile_image'), 'r+'));

        //Store $filenametostore in the database
    }

    return redirect('images')->with('status', "Image uploaded successfully.");
}

‘profile_image’ is the name of our file input. We build the unique name of our file and then uploading it to an external server. Notice we have used Storage::disk(‘ftp’) method. This function would store our file to the path as defined in the configuration. A user should store the value ‘$filenametostore’ in your database.

Retrieve or Deleting Image

We are dealing with the images so obviously, we need to display these images on the website. As we are storing these images on the known external server so a user should know the HTTP path of the folder where we store the images. It can be something like YOUR_DOMAIN_URL/images/. In the controller method, we should store the name of the image in the database. So our HTML img tag will as follows:

<img src="YOUR_DOMAIN_URL/images/FILE_NAME" />

Here FILE_NAME is the your image name stored in the database. User can delete the file by the following way:

Storage::disk('ftp')->delete('FILE_NAME');

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “How to Upload Images to Another Server Through FTP in Laravel

Leave a Reply

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