2017-03-06 24 views
0

我現在的問題是,當單擊表中的一個環節,是被觸發的模態不顯示在PHP代碼中給出一個.txt文件。在PHP語句嵌套在HTML <p><pre>結款,並使用fopenfread方法。該代碼一旦執行,只顯示一個空框。.txt文件沒有顯示在引導模式了

<div id=<?php echo "$id"; ?> class="modal fade" aria-hidden="true" style="display: none;"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
    <div class="modal-header"><button aria-hidden="true" class="close"data-dismiss="modal" type="button">×</button> 

     <h4 class="modal-title"><?php echo "$first_name $last_name"; ?></h4> 
      </div> 

    <div class="modal-body"><img src="empty_profile.gif" width="200px" height="200px" alt="Pic goes here"><br> 
    <a href="mail goes here" target="_blank"><i class="fa fa-envelope"></i>&nbsp; Mail</a>  
    <p> 
     <pre> 
     <?php 
      $file = fopen("358.txt", "r") or die("txt not found!!1"); 
      $read=fread($file); 
      echo "$read"; 
      fclose($file);?> 
     </pre>  
    </p>  
    </div> 

的PHP文件的最佳地點的I/O,在我看來,是「模態體」級DIV中,爲模態的格式結構將只允許它是後右頭。我如何獲取文件內容以顯示在我的模式中?

+0

如果你想使用'fread'您需要的文件的長度或塊迭代,直到文件的末尾被擊中。 – Scuzzy

+1

怎麼了這一切'回聲「$ somevar」'?你並不需要用引號括變量 – Phil

回答

4

您錯誤地使用了fread(您錯過了length參數)。

相反,做的documentation建議和使用file_get_contents,而不是...

注意
如果你只是想獲得一個文件的內容轉換爲字符串,使用file_get_contents()因爲它比上面的代碼更好的性能。

例如

<pre> 
    <?= is_readable('358.txt') ? file_get_contents('358.txt') : 'txt not found!!1' ?> 
</pre> 
+0

哦,上帝沒有不使用PHP的短代碼! :) – ATechGuy

+0

@keaner不知道你在說什麼。 *「短代碼」*? – Phil

+1

'<?='代替'<?PHP的echo'。我理解厭惡短標籤('<?'),因爲它們只在PHP安裝時特別啓用才起作用,但自從5.4版本開始,無論設置如何,<?='都應該可以工作。在這一點上,這確實是一個偏好問題。 – ppajer