首页 > 解决方案 > SFTP 上的文件在开头显示附加字符

问题描述

我在 symfony 3.4 中有一个函数,它会抛出文件中不存在的字符:

use Exception;
use phpseclib\Net\SFTP;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\VarDumper\VarDumper;

public static function getInfoFile(string $fileName, ContainerInterface $container)
{
    $host = $container->getParameter('host');
    $sftp = new SFTP($host);
    if (!$sftp->login($container->getParameter('user'), $container->getParameter('pass'))) {
        throw new Exception('Cannot connect to ' . $host);
    }
    $file = $sftp->get("/info/$fileName");

    vardumper::dump($file); // See Response below

    $file = preg_split('/\R/', $file);
    reset($file);

    // vardumper::dump($file); // This would now return each array element prepended with b"""

    return $file;
}

这将返回:

第 30 行的 Service.php:b""" A;B;C;D;E;F\r\n 1;2;3;4;5;6\r\n

此 b""" 不在文件中。我尝试使用 Notepad++ 和 Excel 打开,但看不到。

当我尝试使用 substr 时, b""" 会保留并剪切真实文件。

我究竟做错了什么?我想将每一行的 csv 文件读入一个没有这些神秘 b""" 的数组

标签: phpsymfonycsvsftpphpseclib

解决方案


我找到了解决方案....

$file = $sftp->get("/info/$fileName");
$file = mb_convert_encoding($file, "UTF-8", "ISO-8859-15" ); // Add this

问题是编码。


推荐阅读