CURL – SFTP from PHP

Sample code to do a sftp from php

$ch = curl_init();

$localfile = ‘export-3.csv’;

$fp = fopen($localfile, ‘r’);

curl_setopt($ch, CURLOPT_URL, ‘sftp://user:pass@ftp.remote.com/’.$localfile);

curl_setopt($ch, CURLOPT_UPLOAD, 1);

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);

curl_setopt($ch, CURLOPT_INFILE, $fp);

curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

curl_exec ($ch);

$error_no = curl_errno($ch);

curl_close ($ch);

if ($error_no == 0) {

$error = ‘File uploaded succesfully.’;

} else {

$error = ‘File upload error.’;

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s