<?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[[获取行数]使用PHP将大文件导入到数据库中，PHP的stream_get_line函数读取大文件获取文件的行数的方法行。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Fri, 09 May 2014 07:09:33 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	php 一行行读取文本文件：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
function readLine($file, $line_num, $delimiter=&quot; &quot;)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;/*** set the counter to one ***/
&nbsp;&nbsp;&nbsp;&nbsp;$i = 1;
&nbsp;&nbsp;&nbsp;&nbsp;/*** open the file for reading ***/
&nbsp;&nbsp;&nbsp;&nbsp;$fp = fopen( $file, &#039;r&#039; );
&nbsp;&nbsp;&nbsp;&nbsp;/*** loop over the file pointer ***/
&nbsp;&nbsp;&nbsp;&nbsp;while ( !feof ( $fp) )
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*** read the line into a buffer ***/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$buffer = stream_get_line( $fp, 1024, $delimiter );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*** if we are at the right line number ***/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( $i == $line_num )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*** return the line that is currently in the buffer ***/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $buffer;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*** increment the line counter ***/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*** clear the buffer ***/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$buffer = &#039;&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return false;
&#125;
?&gt;
</textarea><br/><br/>PHP获取文件行数：<br/>背景：<br/>下面是获取文件的行数的方法:<br/>一个文件如果知道有几行的话，就可以控制获取一定的行数的数据，然后放入数据库。这样不管的读取大文件的性能，还是写入数据库的性能，都能得到很大的提高了。<br/>下面是获取文件的行数的方法<br/><textarea name="code" class="php" rows="15" cols="100">
$temp_file = &#039;error.log&#039;;
$fp = fopen($temp_file ,&#039;r&#039;) or die(&quot;open file failure!&quot;);
$total_line = 0;
if($fp)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;/* 获取文件的一行内容，注意：需要php5才支持该函数; */
&nbsp;&nbsp;&nbsp;&nbsp;while(stream_get_line($fp, 8192, &quot;&#92;r&#92;n&quot;))&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$total_line++;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;fclose($fp);
&#125;

</textarea><br/>接下来好操作了吧？<br/>以下的程序主要是每次最大入库1000条，余数不足1000的就入余数。<br/>入库10W条数据时间也才几秒，所以说性能是大大滴的好的。<br/><textarea name="code" class="php" rows="15" cols="100">
define(&#039;EACH_NUM&#039;, 1000);/* 每次入库的条数 */
if(!$total_line) die(&#039;no record!&#039;);
$logs = mod(&#039;logs_error&#039;);
$temp = array();
$num = ceil($total_line/EACH_NUM);
$mod = fmod($total_line,EACH_NUM);
for($i=0;$i&lt;$num;$i++)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if(($i+1) == $num &amp;&amp; $mod)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$temp = $logs-&gt;getFileLines($temp_file, $i*EACH_NUM+1, $mod);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$insert_num += $mod;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$temp = $logs-&gt;getFileLines($temp_file, $i*EACH_NUM+1, ($i+1)*EACH_NUM);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$insert_num += EACH_NUM;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/* 入库 */
&nbsp;&nbsp;&nbsp;&nbsp;$logs-&gt;insert($temp);
&#125;
@unlink($temp_file);
echo &quot;Insert Record:&#123;$insert_num&#125; &#92;nSuccess&quot;;
exit();
</textarea><br/><br/>如果你还有更好的操作文件的方法，或者是更好的入库的方法，可以联系我，我们交流下。<br/><br/>来自：http://www.redyun.net/technology/101.html<br/>原创：http://blog.csdn.net/spring21st/article/details/8439172<br/>http://www.wenlingnet.com/index.php/172/<br/>使用PHP将大文件导入到数据库中：<br/>http://www.love4026.org/313613/%E4%BD%BF%E7%94%A8php%E5%B0%86%E5%A4%A7%E6%96%87%E4%BB%B6%E5%AF%BC%E5%85%A5%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD/
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [获取行数]使用PHP将大文件导入到数据库中，PHP的stream_get_line函数读取大文件获取文件的行数的方法行。]]></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>