Uploading files over SFTP using PHP

Transferring files over FTP using PHP is easily done using various PHP functions and cURL. However transferring files over SFTP raises various problems and is not easily supported via PHP and cURL. phpseclib provides a nice library wrapper that enables easy access to the sftp protocol and various cryptography functions. phpseclib is designed to be fully interoperable with OpenSSL and other standardized cryptography programs and protocols.

The following is an example code to upload files to a remote server over SFTP.

<?php
 
/* Set the correct include path to 'phpseclib'. Note that you will need 
   to change the path below depending on where you save the 'phpseclib' lib.
   The following is valid when the 'phpseclib' library is in the same 
   directory as the current file.
 */
set_include_path(get_include_path() . PATH_SEPARATOR . './phpseclib0.3.0');
 
include('Net/SFTP.php');
 
/* Change the following directory path to your specification */
$local_directory = '/localhost/my-files/';
$remote_directory = '/my-files/';
 
/* Add the correct FTP credentials below */
$sftp = new Net_SFTP('your_ftp_url');
if (!$sftp->login('ftp_username', 'ftp_password')) 
{
    exit('Login Failed');
} 
 
 
 
 
/* We save all the filenames in the following array */
$files_to_upload = array();
 
/* Open the local directory form where you want to upload the files */
if ($handle = opendir($local_directory)) 
{
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..") 
        {
            $files_to_upload[] = $file;
        }
    }
 
    closedir($handle);
}
 
if(!empty($files_to_upload))
{
    /* Now upload all the files to the remote server */
    foreach($files_to_upload as $file)
    {
          /* Upload the local file to the remote server 
             put('remote file', 'local file');
           */
          $success = $sftp->put($remote_directory . $file, 
                                $local_directory . $file, 
                                 NET_SFTP_LOCAL_FILE);
    }
}
 
 
?>
Download Code file
Downloads : 2432 / File size : 170.4 kB


10 thoughts on “Uploading files over SFTP using PHP

  1. When i use put method of stp to transfer 30 mb file it breaks in the middle and the page shows fatal Maximum execution time of 30 seconds exceeded. So what is the solution for this to upload the file which is more than 30 mb and less than 100 mb

  2. I would like to know if this is possible just to upload a single file selected using a form. if so where do i change it??

  3. Just use the last section of the code.

    $success = $sftp->put($remote_directory . $file,
    $local_directory . $file,
    NET_SFTP_LOCAL_FILE);

    The local file to be uploaded should be the temp file that PHP creates when a file is uploaded using a form.

  4. “No compatible server to client encryption algorithms found”
    Weird error … FileZilla connects to it fine with SFTP protocol

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>