php - 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""" 的数组
解决方案
我找到了解决方案....
$file = $sftp->get("/info/$fileName");
$file = mb_convert_encoding($file, "UTF-8", "ISO-8859-15" ); // Add this
问题是编码。
推荐阅读
- python - 在每个 terraform 应用上删除旧的并创建新的 ec2 实例
- python - ScrapydWeb:docker-compose 中的连接被拒绝
- sed - sed 读取文件,在特定行之前获取一个块
- tensorflow - DKeras 缺少 1 个必需的位置参数:“输入”
- javascript - 向 Firestore 提交表单时发送电子邮件
- boost - 使用Cmake构建CGAL项目时boost库为空
- hadoop - 如何在 hadoop gcs 连接器中启用 http 请求/响应日志?
- firebase - Firebase 云消息传递 主题数量 主题限制?
- pycharm - PyCharm 在分支开关上禁用开关打开的文件
- ios - 如何在 React Native 中打开 iOS 的 wifi 设置