如何在 php 中读取文件时添加 /r/n
how to add /r/n at the time of file reading in php
我有一个日志文件,它在 Linux OS 中给出了正确的格式,但在 windows 中它失去了格式。无法读取新行字符。我只能在 reading/downloading 文件时进行更改。请提出解决方案
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)处理 linux 样式行结尾 Windows 非常好。
如果不可能,只需使用正则表达式替换行结尾:
替换
readfile($file);
和
$str = file_get_contents($file);
$str = preg_replace('/\r\n|\r|\n/', "\r\n", $str);
echo $str;
需要更多的内存,因为它需要将文件内容存储在内存中。
我有一个日志文件,它在 Linux OS 中给出了正确的格式,但在 windows 中它失去了格式。无法读取新行字符。我只能在 reading/downloading 文件时进行更改。请提出解决方案
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)处理 linux 样式行结尾 Windows 非常好。
如果不可能,只需使用正则表达式替换行结尾:
替换
readfile($file);
和
$str = file_get_contents($file);
$str = preg_replace('/\r\n|\r|\n/', "\r\n", $str);
echo $str;
需要更多的内存,因为它需要将文件内容存储在内存中。