php连接ftp
PHP连接ftp,发现一个很好用的类库phpseclib。英文原文
Connecting to SFTP with PHP
If you need to connect to SFTP using PHP then the simplest approach I’ve found is to use phpseclib, a library of functions for secure communications.
The library is a Composer package so you will need to have Composer installed, then just require the package as usual:-
$ composer require phpseclib/phpseclib
The library provides what it refers to as a “pure-PHP implementation of SFTP” and supports the most widely used versions of SFTP.
To initiate a connection a new object of SFTP is created with the address of the remote server passed to the constructor. To authenticate our SFTP connection we can pass the login credentials using SFTP::login():-
use phpseclib\Net\SFTP;
$sftp = new SFTP('www.example.com');
if (!$sftp->login('username', 'password')) {
throw new Exception('Login failed');
}
If we’ve got a private key we need to use the RSA class so that we can pass the key in place of the password to the SFTP::login() method:-
use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;
$sftp = new SFTP('www.example.com');
$Key = new RSA();
// If the private key has a passphrase we set that first
$Key->setPassword('passphrase');
// Next load the private key using file_gets_contents to retrieve the key
$Key->loadKey(file_get_contents('path_to_private_key'));
if (!$sftp->login('username', $Key)) {
throw new Exception('Login failed');
}
The SFTP class provides methods for SFTP similar to those provided by PHP’s FTPextension. For example, to get a list of files for the current directory we usenlist() (equivalent of ftp_nlist):-
$files = $sftp->nlist();
To change directory use chdir():-
$sftp->chdir('directory_name');
To get a file from the remote server use get():-
$sftp->get('remote_file', 'local_file');
This saves the remote_file to local_file. If the second parameter is left empty then the method returns the contents of the remote file if successful (otherwise it returns false).
To upload a file to the remote server use put():-
$sftp->put('remote_file', 'contents for remote file');
There are many other methods available and can be found in the SFTP class. The class is well documented with thorough comments. The above methods should get you started though.
Hopefully you can see that phpseclib provides a simple way of working with aSFTP connection.
php连接ftp的更多相关文章
- 如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(上传文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- PHPstorm设置连接FTP,进行文件上传、下载、比较
内容转载自:http://www.cnblogs.com/jikey/p/3486621.html 如何在 ...
- 在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...
- java 无法连接ftp服务器(500 OOPS: cannot change directory)
在使用java连接ftp服务器时可能会出现无法连接的情况,检查代码是没有错误的,这时就应该考虑一下服务器端的情况了: 首先用在本地打开命令窗口,输入:ftp ftp服务器IP,窗口会提示你输入用户名密 ...
- Java连接FTP成功,但是上传是失败,报错:Connected time out
Java代码在本机上传文件到FTP服务器的时候成功,但是部署到测试服务器的时候出现,连接FTP成功但是上传失败,并且报Connected time out错误: 测试服务器和FTP服务都在阿里云上:( ...
- shell使用lftp连接ftp和sftp,并可以指定私钥
lftp连接ftp在脚本中可以 lftp -c "open username:password@host:port; ls /Friso/20180822/click/mobile/SUCC ...
- Ftp主动模式和被动模式以及java连接ftp模式设置
Ftp主动模式和被动模式以及java连接ftp模式设置 https://www.cnblogs.com/huhaoshida/p/5412615.html (1) PORT(主动模式) PORT中文称 ...
随机推荐
- 解决JSP页面图片缓存问题
<% String imagepath="D:\\work\\dbUpdate\\src\\main\\webapp\\newyzm.png"; %> <img ...
- mongoDB 用java连接
(1)下载连接java 的驱动包mongo-2.10.1.jar 下载网址 https://github.com/mongodb/mongo-java-driver/downloads (2)导入下载 ...
- ArcGIS Server 10.2 实战(四)格栅动态配色服务
当你的地理处理服务输出的是格栅,那个不可避免地需要为格栅的各类型数据添加不同色彩进行区分,而默认时格栅的色彩是随机的,或者固定死一套着色方案是也显得不够人性化,难以满足多样的客户需求,下面谈谈如何解决 ...
- ruby 疑难点之—— yield 和 yield self
yield 所有的"方法(methods)"隐式跟上一个"块(block)"参数. 块参数也可以明确给定,形式就是在参数前面加一个"&&quo ...
- 【Irrlicht鬼火引擎】 安装配置Irrlicht鬼火引擎
一.下载引擎 官方网站:http://irrlicht.sourceforge.net/ 官方网站需要FQ才能进入,如果不想FQ,可以通过其他下载地址:CSDN下载:http://download. ...
- hdu 3333 树状数组
思路:定义一个map容器用来记录数ai上次出现的位置.将查询区间按右边界升序进行排序,当插入第i个数ai时,pre[ai]+1---->i的区间就会多一个不同的数,其值就是ai,那么可以用upd ...
- Tamperdata工具使用(登陆时就修改用户名),篡改post数据
Tamperdata是firefox上的一款插件,它可以实现篡改数据的功能,这样可以做一些安全的测试验证,他的原理就是在发出请求前,在本地就开始改动数据,下面修改post请求 1.安装Tamperda ...
- Linux 命令 - traceroute: 数据报传输路径追踪
traceroute 工具用于跟踪数据报的传输路径:当数据报从一台计算机传向另一台计算机时,会经过多重的网关,traceroute 命令能够找到数据报传输路径上的所有路由器.通过 traceroute ...
- Memcached学习(一)
1.Memcached是什么? 引用维基百科上得简介,Memcached 是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,目前已被诸如Facebook等许多 ...
- 【oracle】oracle函数-数值函数
一.数值函数 1. mod(m,n) 求余函数 注意:若m或者n为null,则返回null.若n为0,则返回m的值 eg: