1

I've been working at this for the last 2 days to no avail. I've also searched on StackOverflow for a solution but I couldn't find one that works for me.

I'm trying to pull a .csv file from a SFTP server. I found out you cannot do this with a default installation of PHP. I found 2 solutions.

1) Enable the ssh2_sftp extension in my PHP. I couldn't get this to work. I downloaded the required files, put them in my php/ext folder as directed, and modified the line in php.ini as required. Wouldn't work.

2) Use phpseclib. Couldn't get this to work as you need to use composer with it and composer wont load my php.ini because I have curl enabled?

Are there any other solutions for logging into a sftp server?

Appreciate the help.

Cory Nickerson
  • 883
  • 2
  • 11
  • 19

1 Answers1

0

Since it sounds like you have root access on the machine you're working with, why not use scp? It's a native php function so long as you have shell_exec enabled, and the user www-data (or whatever you call your httpd user) has shell access.

<?php
    $connection = ssh2_connect('your_remote_server', 22); //remote connection
    ssh2_auth_password($connection, 'username', 'password'); //authentication
    ssh2_scp_recv($connection, '/remoteServer/whatever/yourFile.csv', '/localServer/yourNewFilename.csv'); //remote file -> local file
?>

NOTE I have used this, but I also store the "username" and "password" in a MySQL database using bcrypt with a minimum cost of 12. More about that Here

Zak
  • 6,976
  • 2
  • 26
  • 48
  • That uses the ssh2_sftp extension I mentioned in my original post. I couldnt seem to get it working. – Cory Nickerson Oct 26 '17 at 15:00
  • Maybe I'm not installing right? The people who come out with these things don't really document them well or provide clear instructions. – Cory Nickerson Oct 26 '17 at 15:07
  • Are you on a Debian distro? Have you tried simply `sudo apt-get install libssh2-1 php-ssh2 -y` and then `sudo service apache2 restart`? – Zak Oct 26 '17 at 17:06
  • I'm currently using on my desktop which is using XAMPP on Windows. – Cory Nickerson Oct 26 '17 at 17:11
  • In that case it's a little less straight forward .. You have to make sure that you install `ssh2` for the version of `PHP` you have, AND you have to be sure that your'e installing for the correct OS (64 bit vs 32). Furthermore, I think (don't quote me) that, `ssh2` for Windows only goes to `PHP 5.5`. – Zak Oct 26 '17 at 17:18