user1471980 user1471980 - 2 months ago 24
PHP Question

how to SFTP get file using cURL with PHP

I am tryin to use the cURL library in PHP to SFTP get file from a remote server as follows. I get a

no such file exists
error. Any ideas what I am doing wrong?

<html>
<body>

<?php
$user="username";
$pass="password";
$filename="/opt/vmstat/vmstat_file";
$c = curl_init("sftp://$user:$pass@server1/$filename");
$fh = fopen($filename, 'r') or die($php_errormsg);
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($c, CURLOPT_FILE, $fh);
curl_exec($c);
curl_close($c);

?>

</body>
</html>


error:

Warning: fopen(="/opt/vmstat/vmstat_file): failed to open stream: No such file or directory in C:\inetpub\wwwroot\sftp.php on line 9

Answer
$c = curl_init("sftp://$user:$pass@server1/opt/vmstat/vmstat_file");
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
$data = curl_exec($c);
curl_close($c);

this worked.

Comments