php读取文件内容的几种方式

1.fread

string fread ( int $handle , int $length )

fread() 从 handle 指向的文件中读取最多 length 个字节。该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况。

fread() 返回所读取的字符串,如果出错返回 FALSE。

<?php
    $filename = "/usr/local/something.txt";
    $handle = fopen($filename, "r");//读取二进制文件时,需要将第二个参数设置成'rb'
    
    //通过filesize获得文件大小,将整个文件一下子读到一个字符串中
    $contents = fread($handle, filesize ($filename));
    fclose($handle);
?>

如果所要读取的文件不是本地普通文件,而是远程文件或者流文件,就不能用这种方法,因为,filesize不能获得这些文件的大小。此时,你需要通过feof()或者fread()的返回值判断是否已经读取到了文件的末尾。

例如:

<?php
    $handle = fopen('http://www.baidu.com', 'r');
    $content = '';
    while(!feof($handle)){
        $content .= fread($handle, 8080);
    }
    echo $content;
    fclose($handle);
?>

或者

<?php
    $handle = fopen('http://www.baidu.com', 'r');
    $content = '';
    while(false != ($a = fread($handle, 8080))){//返回false表示已经读取到文件末尾
        $content .= $a;
    }
    echo $content;
    fclose($handle);
?>

2.fgets

string fgets ( int $handle [, int $length ] )

fgets()从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。

<?php
    $handle = fopen('./file.txt', 'r');
    while(!feof($handle)){
        echo fgets($handle, 1024);
    }
    fclose($handle);
?>

3.fgetss

string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )

跟fgets功能一样,但是fgetss会尝试从读取的文本中去掉任何 HTML 和 PHP 标记,可以用可选的第三个参数指定哪些标记不被去掉

<?php
    $handle = fopen('./file.txt', 'r');
    while(!feof($handle)){
        echo fgetss($handle, 1024, '<br>');
    }
    fclose($handle);
?>

4.file

array file ( string $filename [, int $use_include_path [, resource $context ]] )

将文件内容读入一个数组中,数组的每一项对应文件中的一行,包括换行符在内。不需要行结束符时可以使用 rtrim() 函数过滤换行符。

<?php
    $a = file('./file.txt');
    foreach($a as $line => $content){
        echo 'line '.($line + 1).':'.$content;
    }
?>

5.readfile

int readfile ( string $filename [, bool $use_include_path [, resource $context ]] )

读入一个文件并写入到输出缓冲。返回从文件中读入的字节数。如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息。

<?php
    $size = readfile('./file.txt');
    echo $size;
?>

6.file_get_contents

string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )

将文件读入一个字符串。第三个参数$context可以用来设置一些参数,比如访问远程文件时,设置超时等等。

另外,file_get_contents相对于以上几个函数,性能要好得多,所以应该优先考虑使用file_get_contents。但是readfile貌似比file_get_contents性能好一点(?),因为它不需要调用fopen。

<?php 
    $ctx = stream_context_create(array( 
        'http' => array( 
            'timeout' => 1    //设置超时
            ) 
        ) 
    ); 
    echo file_get_contents("http://www.baidu.com/", 0, $ctx); 
?>

7.fpassthru

int fpassthru ( resource $handle )

将给定的文件指针从当前的位置读取到 EOF 并把结果写到输出缓冲区。

<?php 
    header("Content-Type:text/html;charset=utf-8"); 
    $handle = fopen('./test2.php', 'r');
    fseek($handle, 1024);//将指针定位到1024字节处
    fpassthru($handle);
?>

几个注意事项:

1. 鼓励在处理二进制文件时使用 b 标志,即使系统并不需要,这样可以使脚本的移植性更好。 

2. allow_url_fopen选项激活了 URL 形式的 fopen 封装协议使得可以访问 URL 对象例如文件。默认的封装协议提供用 ftp 和 http 协议来访问远程文件,一些扩展库例如 zlib 可能会注册更多的封装协议。出于安全性考虑,此选项只能在 php.ini 中设置。

3. 如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/91.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。