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"
原文地址:https://www.cnblogs.com/gide/p/4437961.html