PHP读取文件的常见几种方法
近期对PHP中读取文件的几种主流方式进行了梳理,具体内容如下所示。
1. fread
string fread ( int $handle , int $length )
fread() 函数用于从指定的文件句柄中读取最多 length 个字节。当读取达到 length 字节数、遇到文件末尾(EOF)、网络流中数据包可用,或者在打开用户空间流后已读取 8192 个字节时,该函数会停止读取,具体以最先触发的条件为准。
成功时返回读取到的字符串,若发生错误则返回 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 ]] )
功能与 fgets 类似,但 fgetss 会尝试去除读取文本中的 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 ]] )
读取文件并将其内容写入输出缓冲,返回读取的字节数。出错时返回 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 )
从当前文件指针位置读取至 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. 部分保留字(如 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 = "https://www.jb51.net/
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] => https://www.jb51.net/ )
)
几个注意事项:
1. 建议在处理二进制文件时使用 'b' 标志,即便系统并非强制要求,以提升脚本的可移植性。
2. allow_url_fopen 选项激活了 URL 形式的 fopen 封装协议,允许访问如 ftp 和 http 等远程文件对象。部分扩展库(如 zlib)可能注册更多协议。出于安全考虑,此选项仅在 php.ini 中设置有效。
3. 若需打开包含特殊字符(如空格)的 URL,应使用 urlencode() 进行 URL 编码。
以上即为本文的全部内容,希望对各位的学习有所帮助,也欢迎大家继续支持本站。