php实现sftp上传的方法:1、创建代码“class SFTPConnection private $connection...try{...}catch{...}”;2、执行“sftp -oPort=port user@server”语句即可。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php怎么实现sftp上传?
php 实现SFTP上传文件
php 实现sftp文件上传完全可以用php.net 官网中的方式,代码如下:
class SFTPConnection { private $connection; private $sftp; public function __construct($host, $port=22) { $this->connection = @ssh2_connect($host, $port); if (! $this->connection) throw new Exception("Could not connect to $host on port $port."); } public function login($username, $password) { if (! @ssh2_auth_password($this->connection, $username, $password)) throw new Exception("Could not authenticate with username $username " . "and password $password."); $this->sftp = @ssh2_sftp($this->connection); if (! $this->sftp) throw new Exception("Could not initialize SFTP subsystem."); } public function uploadFile($local_file, $remote_file) { $sftp = $this->sftp; $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w'); if (! $stream) throw new Exception("Could not open file: $remote_file"); $data_to_send = @file_get_contents($local_file); if ($data_to_send === false) throw new Exception("Could not open local file: $local_file."); if (@fwrite($stream, $data_to_send) === false) throw new Exception("Could not send data from file: $local_file."); @fclose($stream); } } try { $sftp = new SFTPConnection("localhost", 22); $sftp->login("username", "password"); $sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received"); } catch (Exception $e) { echo $e->getMessage() . "\n"; }
但是在进行中遇到了一个问题, 我的php版本是 PHP 5.6.31 (cli) (built: Aug 2 2017 15:05:23) , 在执行
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
fopen的时候 执行文件 会报 "Segmentation fault" 的错误, 然后变成以下方式便可以解决
$stream = @fopen("ssh2.sftp://" . intval($sftp) . $remote_file, 'w');
其中,在实现sftp上传的时候,没有在意上传文件和上传目录的区别(例如: /upload 和 /upload/test.txt 的问题), 导致每次执行php 都会报 fopen(): Unable to open ssh2.sftp://5/upload on remote host. 问题解决方法就是 认真, 大写的认真
以上就是php做的, 只要登录sftp服务器 进行查看便知道结果.
sftp 命令登录方式:
sftp -oPort=port user@server 然后输入密码, 进去之后可以到相对的目录查看文件是否存在.
推荐学习:《PHP视频教程》
以上是php怎么实现sftp上传的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

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