牛骨文教育服务平台(让学习变的简单)
博文笔记

PHP读取文件所有内容到字符串 file_get_contents

创建时间:2012-12-26 投稿人: 浏览次数:2785

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); 
?>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。