phpseclib 不上传正确的文件内容 [英] phpseclib doesn't upload correct file contents

查看:73
本文介绍了phpseclib 不上传正确的文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以它所做的是成功连接然后上传文件 logo.png 但如果我使用 html 上传代码,文件的内容不是网络主机上的内容或使用 html 上传的内容.它在文件中放置的是第二个 ' 之间的实际文本,因此对于该示例,logo.png 的内容实际上是 logo.png 而不是图片.

So what it does is successfully connects then uploads the file logo.png but the contents of the file isn't what was on web host or uploaded with html if i use a html upload code. What it puts in the file is the ACTUAL text between the second ' 's so for that very example the contents of logo.png is literally logo.png and not the picture.

require_once("ftp/vendor/autoload.php");

use phpseclib\Net\SFTP;

$sftp = new SFTP('SERVER');

if (!$sftp->login('USER', 'PW')) {
    throw new Exception('Login failed');
}

$sftp->put("/some/path/logo.png", "logo.png", NET_SFTP_LOCAL_FILE);

推荐答案

如果您通读文档,您会发现 put() 函数是$data,因此不是文件路径,而是实际要写入的数据:

If you would read through the documentation, you would find out that the second argument of the put() function is $data, therefore not the file path, but the actual data to write:

function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)

默认情况下,NetSFTP::put() 不从本地文件系统读取.$data 直接转储到 $remotefile.[...]

By default, NetSFTP::put() does not read from the local filesystem. $data is dumped directly into $remotefile. [...]

要上传本地文件,最简单的方法是将内容读入一个变量中,该变量将传递给 put() 函数:

To upload a local file, the easiest way is to read the content into one variable that will be passed to the put() function:

$data = file_get_contents("logo.png");
$sftp->put("/some/path/logo.png", $data);

您可能正在使用新版本的 phpseclib,其中 重命名这些常量 使它们更像对象.对于新版本,您应该使用

You are probably using a new version of phpseclib, which renamed these constants to make them more object-like. With a new version, you should use

$sftp->put("/some/path/logo.png", "logo.png", SFTP::SOURCE_LOCAL_FILE);

这篇关于phpseclib 不上传正确的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆