Transfer files from one server to another server using FTP in PHP

In this article we are using phpseclib PHP library to transfer file from one server to another. To transfer files from one server to another using SFTP in PHP, you can use the SFTP library built into PHP.

First, you will need to connect to the server using Net_SFTP() , then you will need to login using login() function.

Once connected and logged in, you can use put() function to upload a file from your local server to the remote server and get() function to download a file from the remote server to your local server.

Here is an example of how to upload a file from the local server to a remote server using FTP in PHP:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.theknowledgeadda.com');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}


// where x is the size of filename.txt
$sftp->put('remote.txt', 'local.txt', NET_SFTP_LOCAL_FILE);
?>

Leave a Reply

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