这篇文章主要为大家详细介绍了PHP SFTP实现上传下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一、SFTP介绍:
使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议。区别:sftp是ssh内含的协议(ssh是加密的telnet协议), 只要sshd服务器启动了,它就可用,而且sftp安全性较高,它本身不需要ftp服务器启动。 sftp = ssh + ftp(安全文件传输协议)。由于ftp是明文传输的, 没有安全性,而sftp基于ssh,传输内容是加密过的,较为安全。目前网络不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。
sftp这个工具和ftp用法一样。但是它的传输文件是通过ssl加密了的,即使被截获了也无法破解。而且sftp相比ftp功能要多一些,多了一些文件属性的设置。
二、SSH2扩展配置
根据自己的php版本选择 扩展包,这里我使用的是php5.3,所以我下载的是 php_ssh2-0.12-5.3-ts-vc9-x86.zip(下载链接)
2. 解压完后,会有三个文件,libssh2.dll、php_ssh.dll、php_ssh2.pdb。
3. 将 php_ssh.dll、php_ssh2.pdb 放到你的 php 扩展目录下 php/ext/ 下。
4. 将libssh2.dll 复制到 c:/windows/system32 和 c:/windows/syswow64 各一份
5.在 php.ini中加入 extension=php_ssh2.dll
6.重启Apache, 打印phpinfo(); 会出现 SSH2 扩展,表示安装成功
三、SFTP 代码DEMO
调用代码
$config = array( 'host' =>'211.*.*.*', //服务器 'port' => '23', //端口 'username' =>'test', //用户名 'password' =>'*****', //密码 ); $ftp = new Sftp($config); $localpath="E:/www/new_20170724.csv"; $serverpath='/new_20170724.csv'; $st = $ftp->upftp($localpath,$serverpath); //上传指定文件 if($st == true){ echo "success"; }else{ echo "fail"; }
SFTP 封装类
<?php /** * SFtp上传下载文件 * */ namespace Common\ORG\Util; class Sftp { // 初始配置为NULL private $config = NULL; // 连接为NULL private $conn = NULL; // 初始化 public function __construct($config) { $this->config = $config; $this->connect(); } public function connect() { $this->conn = ssh2_connect($this->config['host'], $this->config['port']); if( ssh2_auth_password($this->conn, $this->config['username'], $this->config['password'])) { }else{ echo "无法在服务器进行身份验证"; } } // 传输数据 传输层协议,获得数据 public function downftp($remote, $local) { $ressftp = ssh2_sftp($this->conn); return copy("ssh2.sftp://{$ressftp}".$remote, $local); } // 传输数据 传输层协议,写入ftp服务器数据 public function upftp( $local,$remote, $file_mode = 0777) { $ressftp = ssh2_sftp($this->conn); return copy($local,"ssh2.sftp://{$ressftp}".$remote); } }
以上是php实例之SFTP实现上传下载功能的详细内容。更多信息请关注PHP中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境