2016-11-22 36 views
0

我有一個日誌文件,它在Linux操作系統中給出了正確的格式,但在Windows中它沒有格式化。新行字符無法讀取。我只能在閱讀/下載文件時進行更改。請建議解決方案如何在文件讀取時添加/ r/n php

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; 
    } 
} 
+0

'\ r \ n'不是'/ r/n'。 – v7d8dpo4

回答

0

而不是編輯日誌文件我會建議讓一個不同的編輯器來查看該文件。大多數編輯器(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; 

需要多一點的更多的內存,因爲它需要將文件內容存儲在內存中。

+0

感謝您的評論,但其他團隊使用記事本,我們不能強迫他們改變一般的做法。 – NGupta

+0

@NGupta看到我的編輯 – Sven

+0

謝謝我會檢查和更新。 – NGupta