2016-08-03 219 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并将其重命名为文本文件,它完美的工作!