string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )
将文件读入一个字符串。第三个参数$context可以用来设置一些参数,比如访问远程文件时,设置超时等等。
另外,file_get_contents相对于以上几个函数,性能要好得多,所以应该优先考虑使用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);
?>
本文介绍PHP的file_get_contents函数,用于将文件内容读取为字符串。该函数支持使用include_path,可配置上下文参数,并且通常比其他文件读取函数更具性能优势。虽然readfile可能在某些情况下性能更优,但file_get_contents因简洁和高效而常被推荐。
614

被折叠的 条评论
为什么被折叠?



