readfile & file_get_contents异同

Posted 余磊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了readfile & file_get_contents异同相关的知识,希望对你有一定的参考价值。

记录一下:应用memcache时,准备把整个文件缓存到内存中,遇到了比较奇怪的事情,因为最初使用readfile来读取文件,结果这个函数返回一个字节数,而不是一个字符串,于是文件没办法再输出,最后使用file_get_contents解决问题。
 
file_get_contents -- 将整个文件读入一个字符串
 
说明
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )
和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。
 
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。
 
readfile -- 输出一个文件
 
说明
 
int readfile ( string filename [, bool use_include_path [, resource context]] )
读入一个文件并写入到输出缓冲。
 
返回从文件中读入的字节数。如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息。
 
$file = readfile(‘./test.txt‘);
echo $file;
var_dump($file);
 
$file = file_get_contents(‘./test.txt‘);
echo $file;
var_dump($file);
 
输出:
 
qweyrqiwueryqioweyrqioweru26
int(26) 
qweyrqiwueryqioweyrqioweru
string(26) "qweyrqiwueryqioweyrqioweru"

以上是关于readfile & file_get_contents异同的主要内容,如果未能解决你的问题,请参考以下文章

使用Image header() & readfile()在PHP页面上显示一个Base64图像。

在 codeigniter 中使用控制器中的 readfile() 显示图像

如果我有完整路径,如何使用 Filesystem.readFile

串口:ReadFile 和 CloseHandle [重复]

在C代码中使用ReadFile

node.js每日灵魂一问- node.js的fs模块&常用方法