如何在php中读取文件时添加/ r / n

NGupta

我有一个日志文件,该文件在Linux OS中提供了正确的格式,但是在Windows中它会丢失格式。换行符无法读取。我只能在读取/下载文件时进行更改。请提出解决方案

if(isset($_REQUEST['download'])) {
    $file = $dir . "/" . basename($_REQUEST['download']);
    if (file_exists($file)) {
        ob_start();
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header("Pragma: no-cache");
        header("Expires: 0");
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}
斯文

建议不要使用其他编辑器来查看该文件,而不是编辑日志文件。大多数编辑器(sublime,atom,notepad ++,vscode)在Windows中都能很好地处理Linux样式的行尾。

如果不可能,只需使用正则表达式替换行尾即可:

代替

readfile($file);

$str = file_get_contents($file);
$str = preg_replace('/\r\n|\r|\n/', "\r\n", $str);
echo $str;

由于需要将文件内容存储在内存中,因此会占用更多的内存。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章