Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload files to SFTP server via PHP (phpseclib)

I have CSVs that I want to upload to the 'incoming' folder in the SFTP server. I am using phpseclib to do this. The connection is already there but it does not output anything.

I'm not sure if what I did was correct since I haven't dealt with SFTP before. Here's what my code looks like:

$file = "leads.csv";

$server = "41.160.150.200";
//$server = "ft.bayport.co.za";
$port = "22";
$username = "";
$password = ""; 

//username and password removed for security reasons

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

include 'Net/SFTP.php';

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX); // or NET_SFTP_LOG_SIMPLE

$sftp = new Net_SFTP($server);

// Check SFTP Connection
if (!$sftp->login($username, $password)) {
    echo 'Login Failed.';
    echo $sftp->getSFTPLog();
}else{

echo 'Connected to SFTP.';

echo $sftp->pwd();

// Upload CSVs to SFTP incoming folder
     echo $upload = $sftp->put("incoming/".$file, "./bayport/".$file, NET_SFTP_LOCAL_FILE);

}

I would really appreciate any help. Thanks!

like image 456
maikelsabido Avatar asked Feb 25 '14 06:02

maikelsabido


People also ask

What is SFTP PHP?

Learn how to connect to SFTP, list files, upload and download using the PHP programming language. Guides Engineering. SFTP is a standard and secure protocol through which parties can safely transfer and share data and files. In any case, engaging with an SFTP server programatically can be challenging.

What is SFTP folder?

sFTP (secure File Transfer Program) is a secure and interactive file transfer program, which works in a similar way as FTP (File Transfer Protocol). However, sFTP is more secure than FTP; it handles all operations over an encrypted SSH transport.

How install Phpseclib on Windows?

Download phpseclib library and include into your project directory. In the relevant php file add following code. Show activity on this post. set_include_path(get_include_path() .


1 Answers

Now I know what's the problem with the script.

The remote directory URL was wrong. "incoming/" should be "/incoming/"

like image 122
maikelsabido Avatar answered Oct 10 '22 18:10

maikelsabido