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中文称 ...
随机推荐
- How to Diagnose Audi Vehicles via Tuirel S777
Here share how to diagnose Audi cars via Tuirel S777. 1.Well connect Tuirel S777 to your Audi car, a ...
- ASP.NET MVC 4 让数据库自动迁移
今天实际测试了下这个方法,可以保持数据库与实体类同步,同时不会出现数据库迁移的提示.但是只能更改实体类来改变数据库,而不能改数据库来改变实体类.所以这才是Code frist,如果通过改数据库表来改动 ...
- NSFileManager文件管理
前提,用到的东东: 1.文件数据类:NSData类型(二进制) 1)作用:专门用于将数据封装成二进制的类.数据(文本,图片,音频,视频....) ==> NSData类型的对象 2)编码方式: ...
- Android 自学之对话框
Android为我们提供了丰富的对话框支持,提供了四种常用的对话框: AlertDialog:功能丰富.实际应用最广泛的对话框. ProgressDialog:进度对话框,该对话框只用于简单的进度条封 ...
- [转]网站优化-IIS7下静态文件的优化
本文转自:http://www.cnblogs.com/Leung/archive/2009/10/26/1590256.html 在网站开发过程中,通常我们会对网站的静态文件做处事,像图片文件,CS ...
- Android中将Bitmap对象以PNG格式保存在内部存储中
在Android中进行图像处理的任务时,有时我们希望将处理后的结果以图像文件的格式保存在内部存储空间中,本文以此为目的,介绍将Bitmap对象的数据以PNG格式保存下来的方法. 1.添加权限 由于是对 ...
- 【策略】UVa 1344 - Tian Ji -- The Horse Racing(田忌赛马)
Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a high ...
- JavaScript部分总结
一.词法结构 1.js里面区分大小写 2.注释分为两类: // 单行注释 /*多行注释*/ 3.字面量(直接量 literal) 12 ...
- 你所不知道的z-index的用法
在开始今天的内容之前,先让我们来看下面一段代码: <style type="text/css"> #div1,#div2{ width:200px; height:20 ...
- 随机生成MyEclipse注册码
package com.registercode; import java.io.BufferedReader;import java.io.IOException;import java.io.In ...