I have a log file which gives correct formatting in Linux OS, but in windows it looses formatting. New line characters can't get read. I can only make changes at the time of reading/downloading file. Please suggest solution
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;
}
}
Instead of editing the log file I would suggest getting a different editor to look at the file. Most editors (sublime, atom, notepad++, vscode) handle linux style line endings in Windows perfectly fine.
If that is not possible, simply use a regex to replace the line endings:
Replace
readfile($file);
with
$str = file_get_contents($file);
$str = preg_replace('/
||
/', "
", $str);
echo $str;
Takes a bit more memory, since it needs to store the file content in the memory.