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的更多相关文章

  1. 如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作

    Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...

  2. 【FTP】C# System.Net.FtpClient库连接ftp服务器(上传文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  3. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  4. PHPstorm设置连接FTP,进行文件上传、下载、比较

                                                 内容转载自:http://www.cnblogs.com/jikey/p/3486621.html   如何在 ...

  5. 在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作

    Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...

  6. java 无法连接ftp服务器(500 OOPS: cannot change directory)

    在使用java连接ftp服务器时可能会出现无法连接的情况,检查代码是没有错误的,这时就应该考虑一下服务器端的情况了: 首先用在本地打开命令窗口,输入:ftp ftp服务器IP,窗口会提示你输入用户名密 ...

  7. Java连接FTP成功,但是上传是失败,报错:Connected time out

    Java代码在本机上传文件到FTP服务器的时候成功,但是部署到测试服务器的时候出现,连接FTP成功但是上传失败,并且报Connected time out错误: 测试服务器和FTP服务都在阿里云上:( ...

  8. shell使用lftp连接ftp和sftp,并可以指定私钥

    lftp连接ftp在脚本中可以 lftp -c "open username:password@host:port; ls /Friso/20180822/click/mobile/SUCC ...

  9. Ftp主动模式和被动模式以及java连接ftp模式设置

    Ftp主动模式和被动模式以及java连接ftp模式设置 https://www.cnblogs.com/huhaoshida/p/5412615.html (1) PORT(主动模式) PORT中文称 ...

随机推荐

  1. laraval框架model注意事项

    今天创建了一个model,名字叫做Role_Users.php,结果运行的时候死活不识别,后来发现去掉名字中的下划线就可以识别了,虽然解决了,但还是不明白原理,若有大神看到但求解答

  2. http协议Authorization认证方式在Android开发中的使用

    我们都知道,http协议是一种无状态协议,在Web开发中,由于Session和Cookie的使用,使得服务端可以知道客户端的连接状态,即用户只需要在浏览器上登录一次,只要浏览器没有关闭,后续所有的请求 ...

  3. struts2.1笔记02:servlet简介

    1.     Servlet 是在服务器上运行的小程序.这个词是在 Java applet的环境中创造的,Java applet 是一种当作单独文件跟网页一起发送的小程序,它通常用于在客户端运行,结果 ...

  4. MPMoviePlayerController过期导致视频播放时间条颠倒及AVPlayerViewController的用法

    MPMoviePlayerController虽然好用 但是过期了 所以可能会导致一些莫名的问题  ,比如说下面时间条的问题 但我们可以使用AVPlayerViewController来实现相应的想过 ...

  5. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  6. [未完成][Mooc]关于IO总结

    整个课程的大纲:1.编码问题.2.File类的使用3.RandomAccessFile的使用4.字节流的使用.5.字符流的使用.6.对象的序列化和反序列化. 视频1:文件的编码Eclipse的一大特点 ...

  7. fadein()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JavaScript面试问题:事件委托和this

            JavaScript不仅门槛低,而且是一门有趣.功能强大和非常重要的语言.各行各业的人发现自己最混乱的选择是JavaSscript编程语言.由 于有着各种各样的背景,所以不是每个人都对 ...

  9. MyBatis(3.2.3) - Paginated ResultSets using RowBounds

    Sometimes, we may need to work with huge volumes of data, such as with tables with millions of recor ...

  10. C#算法基础之插入排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...