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.
/* 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);
}
}
?>
$sftp = new Net_SFTP(‘your_ftp_url’);
Can an IP be placed here?
You can use an IP address
I like your blog, do you have a Facebook or Twitter page where I can follow you?
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
Unable to download more than 64 kb, what setting should i change.
$sftp->get($remotefile,$localfile);
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??
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.
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?
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.
“No compatible server to client encryption algorithms found”
Weird error … FileZilla connects to it fine with SFTP protocol
Notice: Cannot connect to 200.62.171.29. Error 110. Connection timed out in /var/www/uat/Bvl/sftp/phpseclib0.3.0/Net/SSH2.php on line 776 Login Failed
There are a number of reasons this can occur. Try this link below:
https://github.com/phpseclib/phpseclib/issues/390