How to Transfer Files to Remote Server in PHP

Storing files or images on an external server is a common behavior nowadays. People use cloud storage like AWS, Azure, Google Cloud, Backblaze, or their own remote server to store the files. In that scenario, user store files on another server and call it through the link on their application server. The benefit of doing this is it saves bandwidth on a server which results in better server performance.

In the past, while working on a client’s project we followed this technique for storing images on a remote server. What the client wanted was to send images to their own external server and call it from an application wherever needed. In that case, we needed to store the remote image path in the database after uploading it to a remote server.

In this article, we will show you how to transfer files to a remote server in PHP through FTP. For the sake of this tutorial, we use images for uploading. The user can choose any type of file as per their requirement.

Getting Started

To proceed further, you should have the FTP credentials of your server. Another requirement is the Composer. Make sure you have installed Composer on your system. You are going to use package called Flysystem which require Composer for installation.

Open the terminal in your project root directory and run the command below.

composer require league/flysystem

Upon installation, create an HTML form with file input and submit button. This form will use to browse the image and send it to the server.

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="image" /><br>
    <input type="submit" name="submit" value="Submit">

Transfer Files to Remote Server

You have installed the Flysystem library and also created an HTML form. The next thing that needs to do is take the image from file input and send it to a remote server. Your upload.php will contain the below code.

require_once "vendor/autoload.php";

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as Adapter;

if(isset($_POST['submit'])) {

    if(isset($_FILES['image']['name']) && !empty($_FILES['image']['name'])) {

        try {
            $filesystem = new Filesystem(new Adapter([
                'host' => 'HOST_NAME_HERE',
                'username' => 'USERNAME_HERE',
                'password' => 'PASSWORD_HERE',

            $stream = fopen($_FILES['image']['tmp_name'], 'r+');
            $filesystem->putStream('public_html/images/'.$_FILES['image']['name'], $stream);

            if (is_resource($stream)) {

            echo "Image uploaded on external server successfully.";
        } catch(Exception $e) {
            echo $e->getMessage();

Replace the placeholders with the actual values. Here we are storing images inside public_html/images directory. You can adjust this path as per your requirement. In the above code, Flysystem connects to your server through FTP, reads the image content and puts the image on the specified path.

We hope you got to know about how to transfer files to the remote server in PHP. Using Flysystem, you can also transfer files on AWS S3, Azure, Dropbox, etc. You can learn more about this package on their official documentation.

Related Articles

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

Leave a Reply

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