Server-to-Server File Transfer with PHP Script

When migrating a website, transferring files from the source server to the destination server is a key step. If you have SSH access, you can use commands like rsync, scp, or FTP to move the files efficiently.

However, without SSH access, the process becomes more tedious. You’d need to download the files to your local computer first and then upload them to the new server. This method not only consumes time but also uses up bandwidth, which can be a significant drawback, particularly for large files or slower internet connections.

This PHP script simplifies the process by allowing you to transfer files directly from the source server to the destination server without needing intermediate downloads. Using PHP’s cURL functions, you can download backups from the source server to the destination server, saving both time and bandwidth.

<?php
# Author: ServerOK
# Web: https://serverok.in/server-to-server-file-transfer-with-php-script

# URL of the file to be downloaded
$source_url = 'https://example.com/backup.zip';

$filename = basename(parse_url($source_url, PHP_URL_PATH));
$destination_path = __DIR__ . '/' . $filename;
set_time_limit(0);

function download($source, $destination) {
    $ch = curl_init($source);
    $fp = fopen($destination, 'w+');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
    $success = curl_exec($ch);
    if ($success === false) {
        echo 'cURL Error: ' . curl_error($ch);
        return false;
    }
    curl_close($ch);
    fclose($fp);
    return true;
}

$success = download($source_url, $destination_path);

if ($success) {
    echo '<p style="color: green; font-weight: bold; font-size: 16px;">File transferred successfully to ' . htmlspecialchars($destination_path) . '</p>';
} else {
    echo '<p style="color: red; font-weight: bold; font-size: 16px;">File transfer failed</p>';
}

How to Use the Script

1) Create a Backup on the Source Server

Log in to your source server’s control panel and create a compressed archive (zip or tar.gz) of your website’s document root, usually “public_html” or “httpdocs”, depending on your server setup. Move this file to the document root so that it can be accessed via a URL, like:

https://example.com/backup.zip

2) Prepare the Destination Server

Log in to your destination server and create a file named download.php. Paste the PHP code above into the file. In the script, locate the line:

$source_url = 'https://example.com/backup.zip';

Replace the URL with the link to your backup file.

3) Run the PHP Script on the Destination Server

Run the script on your destination server to download the backup file and store it in the same folder as the script. Make sure you perform this step while the domain still points to the source server. If the domain is already pointed to the new server, the download link will no longer work.

To run the download.php script without changing the nameservers or DNS, you need to modify your computer’s hosts file and add an entry for the domain so your computer will see the site from the new server without DNS change.

NEW-SERVER-IP-HERE yourdomain.com www.yourdomain.com

After editing the hosts file, restart your browser to view the site from the new server. For more details, refer to Preview Website Before Nameserver Change.

Once the file is downloaded to the destination server, you can extract it using the hosting control panel or a PHP exec script. Move the extracted files to the correct directory to make the site work.

If your site uses a MySQL database, you’ll also need to download a database backup from the source server and restore it on the destination server. For large databases, phpMyAdmin may fail to restore properly. You can find a PHP script which help restore database backup using PHP script at How to Restore Large Database with PHP Script

Need help with Linux Server or WordPress? We can help!

Leave a Reply

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