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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.