php 读取txt 显示

<?php
$fp = fopen('welcome.txt', 'r');
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while ($char = fgetc($fp)) {
echo "$char";
}
?>
小白,我想把我的txt文件全部显示,当爱30KB左右,但是我用这段代码,百度知道找到的,不能显示完全,只能显示2-3kb左右,请问下,怎样把源文件全部显示出来嘞?

   推荐使用file_get_content函数读取,再一次性echo

<?php
    $file = 'welcome.txt';
    $content = '';
    if(is_file($file)){
        $content = file_get_content($file);// 一次性取完
    }
    
    
    //echo $content;  //这个是直接echo,一般文字会挤在一起,推荐用下面的
    echo '<pre>'.htmlspecialchars($content) . '</pre>'; // 这个是原格式

追问

谢谢你,我找到了,但是这个没判断,帮我写个判断被,就是当文件不错在的时候!

追答

-_-# lie.....那是说谎的意思,得line

你要写判断?

<?php
    $file = 'D:\baidu.txt';
    if (!is_file($file)) {
        echo 'Could not open file '.$file;
        die; // 记得这里终止
    }
    $content = file_get_content($file);
    // .... 接着写后面的

追问

  亲,不会下,能帮我写个完整的嘛!拜托了

温馨提示:答案为网友推荐,仅供参考
相似回答