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. Mysql中自增字段(AUTO_INCREMENT)的一些常识

    Mysql中自增字段(AUTO_INCREMENT)的一些常识: http://chengxuyuan.naxieshir.com/fenlei/2/p/151.html

  2. Android XML文件布局各个属性详解

    第一常用类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android: ...

  3. [Java] 集合类(List、Set、Map的基本使用)

    数组是一种很常见的数据结构,开始接触编程的时候多数程序都和数组相关.刚开始接触Java时也是一直使用数组写一些程序,后来越来越觉得... 数组是一种很常见的数据结构,开始接触编程的时候多数程序都和数组 ...

  4. Http Statis 500 -错误笔记

    HTTP Status 500 - type Exception report message description The server encountered an internal error ...

  5. python(5)- sys.stdout()实现进度条

    1. 使用\r , 让其始终在行首输出,实现进度条 import sys, time ''' 使用\r 来实现进度条的效果,\r 是光标移到行首但不换行. 假设文件大小为60,一下下载1, 下载到60 ...

  6. python(5)–sys模块

    sys.argv 命令行参数list, 第一个元素是程序本身路径 sys.exit(n) 退出程序,退出时输入信息n sys.version 获取python解释程序的版本信息 sys.maxint ...

  7. 【安卓面试题】使用SQLiteOpenHelper的getReadableDatabase()获得的数据库能不能,做写的操作

    可以! 不要被Readable的意思误导啦,readable是可读的意思,但不代表不能写哦. getReadableDatabase() 会获取用于操作SQLiteDatabase的实例. getRe ...

  8. [改善Java代码]由点及面,一叶知秋----集合大家族

    Java中的集合类实在是太丰富了,有常用的ArrayList.HashMap,也有不常用的Stack. Queue,有线程安全的Vector.HashTable,也有线程不安全的LinkedList. ...

  9. [设计模式]<<设计模式之禅>>工厂方法模式

    1 女娲造人的故事 东汉<风俗通>记录了一则神话故事:“开天辟地,未有人民,女娲搏黄土做人”,讲述的内容就是大家非常熟悉的女娲造人的故事.开天辟地之初,大地上并没有生物,只有苍茫大地,纯粹 ...

  10. The message queue

    今晚来整理一下消息队列,消息队列是一条由消息连接而成的链表,存在内核里面,通过消息对了的引用标识符来访问,每个消息队列都有一个msqid_ds结构与之对应,这个结构保存了消息队列的当前状态参数,这个结 ...