0

I'm trying to get a .csv file from a SFTP server, and then save it locally. I'm using the following code but it doesn't seem to be working.

<?php

$user = 'username';
$pass = 'password';
$host = 'order.domain.com';
$file = 'filename.csv';

$remote = "sftp://$user:$pass@$host/$file";
$local = getcwd() . '/' . $file;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $remote);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($local, curl_exec($curl));
curl_close($curl);

?>

It seems to be creating the filename.csv file in the current working directory, however it is blank.

It appears maybe the curl isn't connecting to the remote server? Maybe the curl isnt set up for SFTP?

j08691
  • 204,283
  • 31
  • 260
  • 272
Cory Nickerson
  • 883
  • 2
  • 11
  • 19

1 Answers1

2

From the documentation:

currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols

Note that SFTP and FTP are, despite the similar names, very different.

If you want SFTP, then you'll need to use a tool designed to work with it. See this question.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    I've already tried to use the SSH2 extention for PHP but I couldn't get it to work. After hours of trying to get it to work I found out it requires 2 other PHP library's to be installed, and when I looked up those librarys they were just a bunch of files with no directions on how to use them. The other solution I had looked into already phpsecl which I couldn't get to work either as it requires composer which needs to run my php.ini which it couldnt do because curl is enabled? – Cory Nickerson Oct 26 '17 at 17:30
  • 1
    Also from what I read cURL does support SSH File Transfer Protocol (SFTP). I had found numerous examples here on stack overflow and a few other blogs, but couldn't get them to work personally. – Cory Nickerson Oct 26 '17 at 17:36