Upload files to SFTP server via PHP (phpseclib)
Asked Answered
E

1

9

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!

Emptyhanded answered 25/2, 2014 at 6:59 Comment(2)
What's the output of that script? Does the login fail or does it successfully connect? Does pwd() return the output that you'd expect? $sftp->put() returns a boolean depending on if the upload succeeded or not. idk if that's what you were expecting?Kielty
Hi @Kielty Thanks for the response. It is successfully connected. I know what's the problem now. "incoming/" should be "/incoming/"Emptyhanded
E
9

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

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

Emptyhanded answered 26/2, 2014 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.