<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>最优良人 &#187; file_get_contents</title>
	<atom:link href="http://www.zui88.com/view-tag/file_get_contents/feed" rel="self" type="application/rss+xml" />
	<link>http://www.zui88.com/blog</link>
	<description>中山php&#124;最优网络</description>
	<lastBuildDate>Mon, 13 May 2013 04:56:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>php file_get_contents函数分段读取大记事本或其它文本文件</title>
		<link>http://www.zui88.com/blog/view-316.html</link>
		<comments>http://www.zui88.com/blog/view-316.html#comments</comments>
		<pubDate>Thu, 22 Dec 2011 03:39:12 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[file_get_contents]]></category>
		<category><![CDATA[fread]]></category>
		<category><![CDATA[文件操作]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=316</guid>
		<description><![CDATA[当我们遇到文本文件体积很大时，比如超过几十M甚至几百M几G的大文件，用记事本或者其它编辑器打开往往不能成功，因为他们都需要把文件内容全部放到内存里面，这时就会发生内存溢出而打开错误，遇到这种情况我们可以使用PHP的文件读取函数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() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。 应用： $str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024); echo $str; 如果针对较小文件只是希望分段读取并以此读完可以使用fread()函数 $fp=fopen('2.sql','r'); while (!feof($fp)){ $str.=fread($fp, filesize ($filename)/10);//每次读出文件10分之1 //进行处理 } echo $str;]]></description>
			<content:encoded><![CDATA[<p>当我们遇到文本文件体积很大时，比如超过几十M甚至几百M几G的大文件，用记事本或者其它编辑器打开往往不能成功，因为他们都需要把文件内容全部放到内存里面，这时就会发生内存溢出而打开错误，遇到这种情况我们可以使用PHP的文件读取函数file_get_contents()进行分段读取。</p>
<h3 class="title">函数说明</h3>
<div class="methodsynopsis"><span class="type">string</span> <span class="methodname"><strong><strong>file_get_contents</strong></strong></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$use_include_path</tt></span> [,  <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$context</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$maxlen</tt></span> ]]]] )</div>
<p class="para">和 <span class="function">file()</span> 一样，只除了  <strong>file_get_contents()</strong> 把文件读入一个字符串。将在参数 <em><tt class="parameter">offset</tt></em> 所指定的位置开始读取长度为 <em><tt class="parameter">maxlen</tt></em> 的内容。如果失败，<strong>file_get_contents()</strong> 将返回  <strong><tt>FALSE</tt></strong>。</p>
<p class="para"><strong>file_get_contents()</strong> 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。</p>
<p class="para">应用：</p>
<p class="para">$str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);<br />
echo $str;</p>
<p class="para">如果针对较小文件只是希望分段读取并以此读完可以使用fread()函数</p>
<p class="para">$fp=fopen('2.sql','r');<br />
while (!feof($fp)){<br />
$str.=fread($fp, <span style="color: #0000bb;">filesize </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$filename</span><span style="color: #007700;">)</span>/10);//每次读出文件10分之1<br />
//进行处理<br />
}</p>
<p>echo $str;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-316.html/feed</wfw:commentRss>
		<slash:comments>263</slash:comments>
		</item>
	</channel>
</rss>
