PHP中file() 函数和file_get_contents() 函数的区别
2012-11-16 16:40
176 查看
PHP中file() 函数和file_get_contents() 函数的作用都是将整个文件读入某个介质,其主要区别就在于这个介质的不同。
file() 函数是把整个文件读入一个数组中,然后将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。
file_get_contents() 函数是把整个文件读入一个字符串中。和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。file_get_contents() 函数是用于将文件的内容
读入到一个字符串中的首选方法。
下面举个例子,以便大家容易理解
假如你现在有个文件是a.txt 如下
hello world
if world is guangmen
shunge ?weiping!
里面那是换行
在file.php里面写入一下代码:
<?php
$arr=file("a.txt");
print_r($arr);
echo "<br/>";
$a=file_get_contents("a.txt");
print_r($a);
?>
在浏览器看对应的效果,如下:
Array ( [0] => hello world [1] => if world is guangmen [2] => shunge ?weiping! )
hello world if world is guangmen shunge ?weiping!
大家清楚了吧
file() 函数是把整个文件读入一个数组中,然后将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。
file_get_contents() 函数是把整个文件读入一个字符串中。和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。file_get_contents() 函数是用于将文件的内容
读入到一个字符串中的首选方法。
下面举个例子,以便大家容易理解
假如你现在有个文件是a.txt 如下
hello world
if world is guangmen
shunge ?weiping!
里面那是换行
在file.php里面写入一下代码:
<?php
$arr=file("a.txt");
print_r($arr);
echo "<br/>";
$a=file_get_contents("a.txt");
print_r($a);
?>
在浏览器看对应的效果,如下:
Array ( [0] => hello world [1] => if world is guangmen [2] => shunge ?weiping! )
hello world if world is guangmen shunge ?weiping!
大家清楚了吧
相关文章推荐
- php中 curl, fsockopen ,file_get_contents 三个函数
- php中 curl, fsockopen ,file_get_contents 三个函数
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- php远程抓取函数curl(),file_get_contents
- php-cgi(php-fpm) CPU 100% 与 file_get_contents 函数的关系
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP中fopen,file_get_contents,curl函数的区别
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP中file()和file_get_contents()的区别(转)
- PHP读取文件函数fread,fgets,fgetc,file_get_contents和file函数的使用总结
- ThinPHP第二十八天(F函数和file_put_contents区别|PHP生成PHP文件,Kindeditor编辑器使用方法)
- php读取本地文件常用函数(fopen与file_get_contents)
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- 深入php函数file_get_contents超时处理的方法详解