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中文称 ...
随机推荐
- c语言_判断例子
例一: #include "stdio.h" int main() { ; if(i) printf("hi"); if(!i) printf("hi ...
- 用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
游戏快要完成了,准备做教学. 我们的教学是在整个界面上盖一层灰色图片,然后把提示点击的按钮部分亮出来,也就是在一块黑色图片上,按需求扣空一小部分.如图,把武器部分扣空,那么在其它地方又会扣空其它部分, ...
- ganymed-ssh2使用
通过maven库获取ganymed-ssh2-262.jar,这是一个实现了ssh2协议的工具包,可以远程连接linux机器,执行命令,有些工作全靠它了 示例代码如下: <!--首先要建立连接, ...
- 关于js中event的target和currentTarget的区别
今天又遇到这个问题了,总是搞不清楚target和currentTarget的区别,百度搜索的时候看到一遍文章解释得很清楚,特意记录下录,以备不时之需: target与currentTarget的区别? ...
- 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项.试图加载格式不正确的程序
.NET:Microsoft Visual Studio 2010 + .NET Framework 3.5 操作系统:windows2008 R2 64 位操作系统 oracle数据库:32位的OD ...
- hdu 4607 树的直径
思路:利用dfs遍历整棵树,找出最长子树与次长子树,两者的和最大就是直径. 若k值小于直径就输出k-1,否则输出(k-d-1)*2+d; #include<iostream> #inclu ...
- Android混淆打包配置总结
Android打包失败出现Proguard returned with error code 1. See console的错误 这个问题是由于代码混淆引起的,找不到引用包. 只需在你的proguar ...
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Android实现双进程守护 (转)
做过android开发的人应该都知道应用会在系统资源匮乏的情况下被系统杀死!当后台的应用被系统回收之后,如何重新恢复它呢?网上对此问题有很多的讨论.这里先总结一下网上流传的各种解决方案,看看这些办法是 ...
- Linux c字符串中不可打印字符转换成16进制
本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的 ...