2017-03-06 93 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