php - SFTP 上的文件在开头显示附加字符

标签 php symfony csv sftp phpseclib

我在 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;
}

返回:

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

这个b"""在文件中不存在。我试过用Notepad++和Excel打开,但看不到。

当我尝试对它使用 substr 时,b"""会保留下来,实际文件会被剪切。

我做错了什么? 我想将每一行的 csv 文件读取到一个数组中,而没有这些神秘的 b"""

最佳答案

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

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

问题是编码。

关于php - SFTP 上的文件在开头显示附加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53958819/

相关文章:

php - 在 PHP 中验证电子邮件地址

php - 使用 sqlite 获得三个不同的搜索结果

templates - 跨继承模板组合 Assetic 资源

Symfony 表单继承 : Neither the property nor one of the methods exist

线程 "main"java.lang.NegativeArraySizeException : -28 中的 Java ArrayList 异常

python - 如何将标题行添加到 Pandas DataFrame

php - 按二级数组计数对数组排序

php - CodeIgniter 和抛出异常

php - 如何在 Symfony 2 中将参数传递给自定义验证约束

c# - CSV 提供的数据源第一列中的奇数字符