File upload via SFTP using php

Download and Extract the PHP library to the same directory level of PHP script to be used for connecting to a remote server via SFTP:
www.phpkode.com/projects/item/php-secure-communications-library/
Use the below code to upload the files to the remote server:
<?php
    //include the library 'phpseclib0.2.1a' downloded and extracted to the same directory level of this script
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.2.1a');
    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');
    }
?>
Instead of uploading files from a local directory to remote server directory, directly text can be written to a new file in the remote server directory.
<?php
    //include the library 'phpseclib0.2.1a' downloded and extracted to the same directory level of this script
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.2.1a');
    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');
    }
?>
NOTE:
  • 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.

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

2 Comments on "File upload via SFTP using php"

  1. vrajesh dave | April 2, 2020 at 6:54 PM | Reply

    Sandeep Agrawal, your example link forward me to wrong link. please check

  2. Hi Vrajesh, Thanks for bringing this up.
    The link is no more valid it seems.

    I think, You can refer below links to achieve file uploads. I will have to check and update my post. 🙂
    https://sourceforge.net/projects/phpseclib/
    http://phpseclib.sourceforge.net/sftp/2.0/examples.html#put
    http://phpseclib.sourceforge.net/

    I hope these links should help you.

Leave a comment

Your email address will not be published.


*