2016-08-03 125 views
0

我知道這聽起來很愚蠢,我實際上想讓PHP讀取另一個php文件作爲文本,因爲我不想在編輯器中打開php,我想用我的瀏覽器顯示它並使用修改JS。這是我所做的: -閱讀PHP文本

$code_to_display = ""; 
     echo "Language Found:".count($project_lang_file)."<br>"; 
      for ($x=0; $x < count($project_lang_file); $x++) { 
       $code_to_display .= readfile($project_lang_file[$x]); 
      //$code_to_display .= file_get_contents($project_lang_file[$x]); 

     echo "Displaying code for $project_lang_file[$x] <br><br>"; 
      echo "<textarea id='myTextarea' style='color:black;'>$code_to_display</textarea><br>"; 
      } 

我試過readfile,fopen,file_get_content,沒有實際顯示它爲文本。任何人都可以幫忙

+0

使用[高亮文件()](http://php.net/manual/en/function.highlight-file.php),它甚至會爲您添加語法高亮顏色 –

+0

@MarkBaker也嘗試過..它顯示爲空以及...不知道爲什麼 – MuthaFury

+0

[編輯一個PHP文件,與另一個PHP文件,使用Fwrite]可能重複(http://stackoverflow.com/questions/11146626/editing-a-php-file -with-another-php-file-using-fwrite) –

回答

-1

試試這個代碼:

$code_to_display = ""; 
echo "Language Found:".count($project_lang_file)."<br>"; 
    foreach ($project_lang_file as $file) { 
     if(file_exists($file)){ 
       $code_to_display = file_get_contents($file); 

       echo "Displaying code for {$file} <br><br>"; 
       echo "<textarea id='myTextarea' style='color:black;'>{$code_to_display}</textarea><br>"; 
     } else { 
       echo "File {$file} not exist" 
     } 
    } 
+0

正如我提到的問題,我試過fopen,file_get_content和readfile,都沒有工作。 – MuthaFury

+0

是的,但不是問題。它可能會導致路徑錯誤,或者超過了內存限制(您將每個文件內容連接在循環中)。還要在你的php.ini中設置E_ALL,並寫入任何錯誤或服務器響應。 – Lakremon

+0

條件是真的,它通過「顯示代碼」,textarea內的結果是空的。 – MuthaFury

0

顯然,我不知道爲什麼使用file_get_content,FOPEN,ReadFile和其他方法都不起作用。但是,我找到了一種方法來解決它通過複製php並將其重命名爲文本文件,它完美的工作!