<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[file_get_contents() 超时错误的解决方法，建议加上timeout，而不是让用户无限等待,如何用file_get_contents来分段读取大文件的方法。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 10 Dec 2008 08:17:07 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	错误：<br/>file_get_contents()&nbsp;&nbsp; failed to open stream: Connection timed out in /data1/www/htdocs/app.space.sina.com.cn/fface/libs/feedsender.class.php<br/><br/>建议加上timeout，而不是让用户无限等待。<br/><div class="code">&lt;?php<br/><br/>$ctx = stream_context_create(array(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;http&#039; =&gt; array(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;timeout&#039; =&gt; 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;)<br/>);<br/><br/>file_get_contents(&quot;http://example.com/&quot;, 0, $ctx); <br/><br/>?&gt;</div><br/><br/>当我们遇到文本文件体积很大时，比如超过几十M甚至几百M几G的大文件，用记事本或者其它编辑器打开往往不能成功，因为他们都需要把文件内容全部放到内存里面，这时就会发生内存溢出而打开错误，遇到这种情况我们可以使用PHP的文件读取函数file_get_contents()进行分段读取。<br/>函数说明<br/>string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )<br/>和 file() 一样，只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败，file_get_contents() 将返回 FALSE。<br/>file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。<br/><br/><textarea name="code" class="php" rows="15" cols="100">
$str = $content=file_get_contents(&quot;2.sql&quot;,FALSE,NULL,1024*1024,1024);
echo $str;
</textarea><br/><br/>如果针对较小文件只是希望分段读取并以此读完可以使用fread()函数<br/><br/><textarea name="code" class="php" rows="15" cols="100">
$fp=fopen(&#039;2.sql&#039;,&#039;r&#039;);
while (!feof($fp))&#123;
$str.=fread($fp, filesize ($filename)/10);//每次读出文件10分之1
//进行处理
&#125;
echo $str;
</textarea><br/><br/>以上就是如何使用file_get_contents函数读取大文件的方法，超级简单。
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] file_get_contents() 超时错误的解决方法，建议加上timeout，而不是让用户无限等待,如何用file_get_contents来分段读取大文件的方法。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>