Skip to content
Home » File upload via SFTP using php

File upload via SFTP using php

Download and Extract the PHP library to the same directory level where the PHP script is to be used for connecting to a remote server via SFTP: https://sourceforge.net/projects/phpseclib

  • Below is the sample code to upload the files to a remote server.
<?php
//include the downloaded library 'phpseclib1.0.20'(1.0.20 at the time of updating this post. please check latest version and library name to use) downloded and extracted to the same directory level of this script.
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib1.0.20');
require_once ('Net/SFTP.php');
//connection parameters
$host = 'host_url';
$port = 'host_port_number';
$username = 'host_user_name';
$password = 'host_password';
//Connect to SFTP host
$sftp = new Net_SFTP($host, $port);
if ($sftp->login($username, $password))
{
    //directory path to source and destination
    $localDirectory = '/local_directory_name/';
    $remoteDirectory = '/remote_upload_directory_name/';
    //save all the filenames in the following array
    $filesToUpload = array();
    //Open the local directory and store all file names in $filesToUpload array
    if ($handle = opendir($localDirectory))
    {
        //loop the local source directory
        while (false !== ($file = readdir($handle)))
        {
            if ($file != '.' && $file != '..')
            {
                $filesToUpload[] = $file;
            }
        }

        closedir($handle);
    }
    //if source directory has any file to upload then upload all files to remote server
    if (!empty($filesToUpload))
    {
        //upload all the files to the remote server
        foreach ($filesToUpload as $file)
        {
            //Upload the local file to the remote server
            $success = $sftp->put($remoteDirectory . $file, $localDirectory . $file, NET_SFTP_LOCAL_FILE);
            if ($success)
            {
                echo '<br>uploaded ' . $file . '<br>';
            }
        }
    }
}
else
{
    exit('Login Failed');
}
?>
  • We can directly write texts on a remote file as well instead of uploading them from a local directory. Use below script for that.
<?php
    //include the downloaded library 'phpseclib1.0.20'(1.0.20 at the time of updating this post. please check latest version and library name to use) downloded and extracted to the same directory level of this script.
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib1.0.20');
    require_once('Net/SFTP.php');
    //connection parameters
    $host = 'host_url';
    $port = 'host_port_number';
    $username = 'host_user_name';
    $password = 'host_password';
    //Connect to SFTP host
    $sftp = new Net_SFTP($host, $port);
    if ( $sftp->login($username, $password) ) {
                //create file on remote server directory and with some text in it
                $success = $sftp->put(
                                      '/remote_upload_directory_name/file_name.text',
                                      'hello, world!'
                                      );
    } else {
        exit('Login Failed');
    }
?>

Notes:

  • phpseclib has pretty much zero server requirements. So long as the server supports PHP it’ll work.
  • ibssh2, in contrast, has to be installed on the server for it to work and a lot of servers don’t have it installed.
    IF it is installed you’re not going to need to include any additional files (whereas with phpseclib you will have to include them but that’s a big IF.

4 thoughts on “File upload via SFTP using php”

    1. Thanks for reporting this. It’s an old article and looks like the library is no more available or has been removed.
      For now, I have removed it from my site and will publish it after updating the article.

Leave a Reply

Your email address will not be published.

0 Shares
Tweet
Pin
Share
Share
Share
%d bloggers like this: