简体   繁体   English

如何从SFTP服务器获取文件

[英]How to get file from SFTP server

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. 我也在StackOverflow上搜索了一种解决方案,但找不到适合我的解决方案。

I'm trying to pull a .csv file from a SFTP server. 我正在尝试从SFTP服务器提取.csv文件。 I found out you cannot do this with a default installation of PHP. 我发现您无法使用默认的PHP安装来执行此操作。 I found 2 solutions. 我找到2个解决方案。

1) Enable the ssh2_sftp extension in my PHP. 1)在我的PHP中启用ssh2_sftp扩展名。 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. 我下载了所需的文件,按照指示将它们放在我的php / ext文件夹中,并根据需要修改了php.ini中的行。 Wouldn't work. 不行

2) Use phpseclib. 2)使用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? 无法将其工作,因为您需要使用composer,而composer不会加载我的php.ini,因为我启用了curl?

Are there any other solutions for logging into a sftp server? 还有其他登录sftp服务器的解决方案吗?

Appreciate the help. 感谢帮助。

Since it sounds like you have root access on the machine you're working with, why not use scp ? 由于听起来您在使用的计算机上具有root访问权限,所以为什么不使用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. 只要您启用了shell_exec ,并且用户www-data (或您所谓的httpd用户)具有shell访问权限,它就是本机php函数。

<?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 注:我已经使用这个,但我还存放了“用户名”和“密码”在MySQL使用数据库bcrypt与12更多关于最低的成本在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM