PHP中读取文件的几种主要方法
1. fread
string fread ( int $handle , int $length )
fread() 函数用于从指定的文件句柄中读取最多 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() 从文件句柄中读取一行,返回长度不超过 length - 1 字节的字符串。读取过程在遇到换行符(含在返回值中)、EOF 或已读取 length - 1 字节时停止,取决于哪种情况先发生。若未指定 length 参数,默认值为 1K(即 1024 字节)。
<?php
$handle = fopen('./file.txt', 'r');
while(!feof($handle)){
echo fgets($handle, 1024);
}
fclose($handle);
?>
注意:自 PHP 4.2.0 起,length 参数变为可选。若忽略该参数,行长默认假定为 1024。从 PHP 4.3 开始,忽略 length 将导致函数持续从流中读取直至行结束。若文件中多数行超过 8KB,显式指定最大行长能更有效地利用资源。此外,自 PHP 4.3 起该函数可安全用于二进制文件,早期版本不支持。
3. fgetss
string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )
fgetss 的功能与 fgets 类似,但会自动去除读取文本中的 HTML 和 PHP 标记。可通过第三个可选参数 $allowable_tags 指定保留哪些标记不被移除。
<?php
$handle = fopen('./file.txt', 'r');
while(!feof($handle)){
echo fgetss($handle, 1024, '
');
}
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 ]] )
readfile() 将文件内容直接写入输出缓冲,并返回读取的字节数。若出错则返回 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 通常具有更优的性能,建议优先使用。不过,有观点认为 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 )
fpassthru() 从当前文件指针位置读取至 EOF,并将结果直接输出到缓冲区。
<?php
header("Content-Type:text/html;charset=utf-8");
$handle = fopen('./test2.php', 'r');
fseek($handle, 1024);//将指针定位到1024字节处
fpassthru($handle);
?>
8. parse_ini_file
array parse_ini_file ( string $filename [, bool $process_sections ] )
parse_ini_file() 用于加载由 filename 指定的 ini 文件,并将其配置项作为关联数组返回。若将 process_sections 参数设为 TRUE,则返回多维数组,包含各节名称及其对应的设置。该参数默认为 FALSE。
注意:
1. 若 ini 文件中的值包含非字母数字字符,必须将其用双引号(")括起来。
2. 部分保留字不能作为 ini 文件的键名,包括 null、yes、no、true 和 false。其中 null、no 和 false 等效于空字符串 "",yes 和 true 等效于 "1"。字符 {}|&~![()" 不得出现在键名中,且在选项值中具有特殊含义。
test.ini文件内容:
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username
test.php内容:
<?php
$config = parse_ini_file('./test.ini', ture);
print_r($config);
?>
输出内容:
Array
(
[first_section] => Array
(
[one] => 1
[five] => 5
[animal] => BIRD
)
[second_section] => Array
(
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
)
几个注意事项:
1. 建议在处理二进制文件时使用 'b' 标志,即便系统并非强制要求,这有助于提升脚本的移植性。
2. allow_url_fopen 选项激活了 URL 形式的 fopen 封装协议,允许访问如 ftp 和 http 等远程文件。某些扩展库(如 zlib)可能注册更多协议。出于安全考虑,此选项仅在 php.ini 中设置有效。
3. 若需打开包含特殊字符(如空格)的 URL,应使用 urlencode() 进行 URL 编码。
以上这篇PHP中读取文件的几个方法总结(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。