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 : 1434 / File size : 170.4 kB

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

9 Responses

1

khaleel

August 8th, 2012 at 12:57 am

$sftp = new Net_SFTP(‘your_ftp_url’);

Can an IP be placed here?

sameer

August 8th, 2012 at 8:37 pm

You can use an IP address

3

Anton

September 13th, 2012 at 4:39 am

I like your blog, do you have a Facebook or Twitter page where I can follow you?

4

kashiraj

September 25th, 2012 at 4:46 am

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

5

bharanikumar

December 20th, 2012 at 4:20 am

Unable to download more than 64 kb, what setting should i change.

$sftp->get($remotefile,$localfile);

6

Sinan

February 10th, 2013 at 1:20 pm

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??

sameer

February 10th, 2013 at 8:28 pm

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.

8

LearnWebDesignOnline

February 14th, 2013 at 10:18 am

I’m getting an error from the $sftp->put command. How to echo the error to page so that I can see what is wrong?

9

Bob Kramer

March 28th, 2013 at 10:58 pm

Is it possible to specify a .pub or .ppk key? I’ve done a quick scan from top to bottom in /NET/SFTP.php and not seeing anything.

Your thoughts