zoukankan      html  css  js  c++  java
  • php使用sftp上传文件

    搞这个SFTP文件传输搞了一整天真是醉了,从sftp安装,到php的ssh2扩展安装,最后到php应用ssh2来上传文件;最后就没有最后了

    Failure creating remote file: (null)  一直报这个错误!网上能搜的基本找了个遍都不起作用,一开始以为laravel框架问题,后来写成原生php脚本也是这个错误,Fine!很好真的没辙了;

    解决方法:

    使用 league/flysystem-sftp 这个而插件来运行SFTP文件传输,在composer下载依赖使用;使用方法也很简单

    use LeagueFlysystemSftpSftpAdapter;
    use LeagueFlysystemFilesystem;
    
    $adapter = new SftpAdapter([
        'host' => 'example.com',
        'port' => 22,
        'username' => 'username',
        'password' => 'password',
        'privateKey' => 'path/to/or/contents/of/privatekey',
        'root' => '/path/to/root',
        'timeout' => 10,
        'directoryPerm' => 0755
    ]);
    
    $filesystem = new Filesystem($adapter);
    //获取到对象之后
    1. $filesystem->put('远程文件路径','内容');
    2. $filesystem->putstream('远程文件路径','文件句柄');//通过fopen获取句柄


    个人备忘:
    linux/ubuntu系统中安装了ssh会自带sftp,如果没有配置的话会一直连接不上远程
    Unable to negotiate with 113.105.66.197 port 8521: no matching host key type found. Their offer: ssh-dss
    Couldn't read packet: Connection reset by peer
    解决方法:
    在/个人目录/.ssh/下面 添加config文件
    vi /个人目录/.ssh/config
    HostkeyAlgorithms +ssh-dss 添加这段话保存退出,在连接一下SFTP试试
  • 相关阅读:
    git branch用法总结
    vue-router异步加载组件
    vue错误提示 Cannot read property 'beforeRouteEnter' of undefined,刷新后跳到首页
    websocket常见错误
    Websocket原理
    怎么在overflow-y:sroll的情况下 隐藏滚动条
    URI和URL有什么区别
    确定浏览器是否支持某些DOM模块
    将nodeList转换为数组(兼容性)
    软件的三种版本
  • 原文地址:https://www.cnblogs.com/cyq632694540/p/7117556.html
Copyright © 2011-2022 走看看