GVKun编程网logo

php 使用file_get_contents读取大文件的方法(php读取超大文件)

13

在本文中,我们将为您详细介绍php使用file_get_contents读取大文件的方法的相关知识,并且为您解答关于php读取超大文件的疑问,此外,我们还会提供一些关于file_get_content

在本文中,我们将为您详细介绍php 使用file_get_contents读取大文件的方法的相关知识,并且为您解答关于php读取超大文件的疑问,此外,我们还会提供一些关于file_get_contents PHP file_get_contents 函数超时的几种解决方法、file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析、file_get_contents PHP下通过file_get_contents的代理使用方法、file_get_contents高級用法,file_get_contents_PHP教程的有用信息。

本文目录一览:

php 使用file_get_contents读取大文件的方法(php读取超大文件)

php 使用file_get_contents读取大文件的方法(php读取超大文件)

《PHP实例:PHP 使用file_get_contents读取大文件的方法》要点:
本文介绍了PHP实例:PHP 使用file_get_contents读取大文件的方法,希望对您有用。如果有疑问,可以联系我们。

当我们遇到文本文件体积很大时,比如超过几十M甚至几百M几G的大文件,用记事本或者其它编辑器打开往往不能成功,因为他们都需要把文件内容全部放到内存里面,这时就会发生内存溢出而打开错误,遇到这种情况我们可以使用PHP的文件读取函数file_get_contents()进行分段读取.
PHP教程

函数说明
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.PHP教程

file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选办法.如果操作系统支持还会使用内存映射技术来增强性能.PHP教程

应用:PHP教程

代码如下:

$str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);
echo $str;

如果针对较小文件只是希望分段读取并以此读完可以使用fread()函数PHP教程

代码如下:

$fp=fopen('2.sql','r');
while (!feof($fp)){
$str.=fread($fp,filesize ($filename)/10);//每次读出文件10分之1
//进行处理
}

echo $str;
PHP教程

以上就是如何使用file_get_contents函数读取大文件的办法,超级简单吧,需要的小伙伴直接搬走!PHP教程

小编培训学院每天发布《PHP实例:PHP 使用file_get_contents读取大文件的方法》等实战技能,PHP、MysqL、LINUX、APP、JS,CSS全面培养人才。

file_get_contents PHP file_get_contents 函数超时的几种解决方法

file_get_contents PHP file_get_contents 函数超时的几种解决方法

这里就简单介绍两种:
一、增加超时的时间限制
这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。
我一开始以为set_time_limit也能影响到file_get_contents,后来经测试,是无效的。真正的修改file_get_contents延时可以用resource $context的timeout参数:

复制代码 代码如下:


$opts = array(
‘http''=>array(
‘method''=>”GET”,
‘timeout''=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents(''http://www.example.com'', false, $context);
fpassthru($fp);


二、一次有延时的话那就多试几次
有时候失败是因为网络等因素造成,没有解决办法,但是可以修改程序,失败时重试几次,仍然失败就放弃,因为file_get_contents()如果失败将返回 FALSE,所以可以下面这样编写代码:

复制代码 代码如下:


$cnt=0;
while($cnt

以上就介绍了file_get_contents PHP file_get_contents 函数超时的几种解决方法,包括了file_get_contents方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

file_get_contents PHP下通过file_get_contents的代理使用方法

file_get_contents PHP下通过file_get_contents的代理使用方法

PHP使用file_get_contents的代理方法获取远程网页的代码。

复制代码 代码如下:

立即学习“PHP免费学习笔记(深入)”;


$url = "http://www.jb51.net/";
$ctx = stream_context_create(array(
''http'' => array(''timeout'' => 5,
''proxy'' => ''tcp://60.175.203.243:8080'',
''request_fulluri'' => True,)
)
);
$result = file_get_contents($url, False, $ctx);
echo $result;
?>


另外一种 curl 的方式使用代理的方法:

复制代码 代码如下:

立即学习“PHP免费学习笔记(深入)”;


function postPage($url)
{
$response = "";
$rd=rand(1,4);
$proxy=''http://221.214.27.253:808'';
if($rd==2) $proxy=''http://222.77.14.56:8088'';
if($rd==3) $proxy=''http://202.98.123.126:8080'';
if($rd==4) $proxy=''http://60.14.97.38:8080'';
if($url != "") {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
$response = curl_exec($ch);
if(curl_errno($ch)) $response = "";
curl_close($ch);
}
return $response;
}


用file_get_contents解决ajax垮域问题
在ajax运用中有时候会垮域调用文件,而浏览器为了安全会默认给这种操作提出警告,甚至直接阻止。如果是IE会弹出一个警告窗口,询问你是否继续操作,只有你同意了IE才会调用垮域的文件。而其它浏览器,如火狐、Opera默认设置下则会直接提示错误,阻止调用外域文件。这会给用户不好的操作体验,如果想通过用户修改浏览器的安全设置来解决这个问题是不现实的,最好是在服务器端解决。
在服务器端可以使用一个同域的文件做为代理文件,这个代理文件将获得外域文件的内容,然后再传递给ajax。这样ajax就不是调用外域文件,而是调用同域的这个代理文件,安全问题也就解决了。
如果你的服务器端支持PHP的话,可以使用file_get_contents这个函数,看到它的名称就已经知道它有获得其它文件内容的功能了。它的详细用法可以参看PHP官方网站上的file_get_contents用法一页,下面是它的简单实例。

复制代码 代码如下:

立即学习“PHP免费学习笔记(深入)”;


$serverAddress = ''http://s.jb51.net'';
//获得外域文件内容
$randomNumber = file_get_contents($serverAddress);
//输出内容
echo $randomNumber;
?>

以上就介绍了file_get_contents PHP下通过file_get_contents的代理使用方法,包括了file_get_contents方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

file_get_contents高級用法,file_get_contents_PHP教程

file_get_contents高級用法,file_get_contents_PHP教程

file_get_contents高級用法,file_get_contents

首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過3次或者5次後就確認為無法連線伺服器而徹底放棄。
這裡就簡單介紹兩種解決方法:

一、增加超時的時間限制

注意:set_time_limit只是設定你的php程式的超時時間,而不是file_get_contents函數讀取url的超時時間。
我一開始以為set_time_limit也能影響到file_get_contents,後來經測試是無效的。真正的修改file_get_contents延時可以用resource $context的timeout參數:
php程式碼

    $opts = array(
        ''http''=>array(
            ''method''=>"get",
            ''timeout''=>60,
        )
    );

    $context = stream_context_create($opts);

    $html =file_get_contents(''http://www.example.com'', false, $context);
    fpassthru($fp);

二、多次嘗試

php程式碼
    $cnt=0;
    while($cnt       $cnt++;
    }

以上方法對付超時已經ok了。接下來演示一下用file_get_contents實現post,如下:
php程式碼

    function post($url, $post = null){
        $context = array();
        if (is_array($post)) {
            ksort($post);

            $context[''http''] = array (
                ''timeout''=>60,
                ''method'' => ''post'',
                ''content'' => http_build_query($post, '''', ''&''),
             );
        }

        return file_get_contents($url, false, stream_context_create($context));
    }

    $data = array (
        ''name'' => ''test'',
        ''email'' => ''test@gmail.com'',
        ''submit'' => ''submit'',
     );

     echo post(''http://www.example.com'', $data);

注意檔案頭的set_time_out否則整個檔案都得超時了

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() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

Note: 如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。

Note: context 参数可以用 NULL 来忽略。
 

怎运用php 的 file_get_contents

file_get_contents() 读取一个文件中的内容,包括远程文件!!
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法!!
echo file_get_contents(''www.baidu.com'');
?>
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/883439.htmlTechArticlefile_get_contents高級用法,file_get_contents 首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過...

关于php 使用file_get_contents读取大文件的方法php读取超大文件的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于file_get_contents PHP file_get_contents 函数超时的几种解决方法、file_get_contents PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析、file_get_contents PHP下通过file_get_contents的代理使用方法、file_get_contents高級用法,file_get_contents_PHP教程的相关知识,请在本站寻找。

本文标签:

上一篇PHP编程:PHP采用get获取url汉字出现乱码的解决方法(php获取url内容)

下一篇php合并数组中相同元素的方法(php合并数组中相同元素的方法有哪些)