2016-04-15 28 views
1

我有這段代碼,我想查看/保存正在顯示的文件(.txt)。 Save的代碼正在工作,但我希望View選項使用警報顯示文件的內容(我嘗試了新的選項卡,但它顯示了我不想發生的地址欄中的路徑)。PHP中的readfile()函數使用alert

問題是它顯示了正在顯示的最後一個文件下的內容,它在警報窗口中顯示的內容只是一個數字。是否可以在警報窗口中顯示文件的全部內容?

$dir = "C:/xampp/htdocs/www/backup"; 

echo "<center>"; 
echo "<h1>Logs</h1>"; 

// Open a directory, and read its contents 
if (is_dir($dir)){ 
    if ($dh = opendir($dir)){ 
    while (($file = readdir($dh)) !== false){ 
     if ($file == '.' or $file == '..'){ continue;} 
     echo $file."<a href='?link=$file'>[VIEW]</a>&nbsp;<a href='?link1=$file'>[SAVE]</a> <br>"; 
    } 
    echo "<br>"; 
    closedir($dh); 
    } 
} 

//View File 
if(isset($_GET['link'])){ 
$link=$_GET['link']; 
    if (is_dir($dir)){ 
     if ($dh = opendir($dir)){ 
      while (($file = readdir($dh)) !== false){ 
       if ($link == $file){ 
         $file = 'C:/xampp/htdocs/www/backup/'.$link; 
         if (file_exists($file)) { 
          //header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
          $read = readfile($file); 
          echo "<script type='text/javascript'> alert($read); </script>"; 
          exit; 
         } 
       } 
      } 
     } 
    } 

} 

回答

0

readfile讀取文件並將其直接寫入輸出緩衝區,即將其直接發送到瀏覽器。例如,將readfile更改爲file_get_contents()

您可能需要將結果字符串換成引號以使其成爲有效的javascript。

$read = file_get_contents($file); 
echo "<script type='text/javascript'> alert('$read'); </script>"; 
exit; 
+0

我也嘗試過file_get_contents,但是當view被點擊時它不會產生警報。 – gooey

+0

OH現在正在工作!非常感謝你!我忘了添加您提到的單引號 – gooey

+0

完成。非常感謝你! – gooey