菜鸟向大家请教个问题 我用PHP写了个页面,用来把一本小说(txt格式)根据文章分成一篇一篇,然后分别把它们写入MYSQL数据库,我调试了一下代码没有错,如果文章比较小的话可以成功运行,但是如果文章太大(5M以上)就有点问题了,大家可以看一下这该怎么解决? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $fp=fopen("../books/$title/$name","r"); //打开文件flock($fp,1);$content='';$zhangjie='';$mode="/第.*章/"; //章$index=0;while(!feof($fp)){ if($index>0) { $line=fgets($fp); //获得行 $temp=trim($line); //去掉首尾空格 if(preg_match($mode,$temp)) { //写入磁盘目录里面 $fpw=fopen("../books/$title/".$index.".txt",'w'); $line.="\r\n"; fwrite($fpw,$line); fwrite($fpw,$content); fclose($fpw); //写入数据库 $sql="select max(idx) from zhengwen where id=$id"; $result=mysql_query($sql); if(!$result) exit; if(mysql_num_rows($result)>=1) { $row=mysql_fetch_row($result); $idx=$row[0]+1; } else { $idx=1; } $sql="insert into zhengwen values('$id','$idx','$type',$zhangjie',now())"; $result=mysql_query($sql); if(!$result) exit; $zhangjie=$temp; $content=''; $index++; } else { $content.=$line; } } else { $line=fgets($fp); $line=trim($line); if(preg_match($mode,$line)) { $zhangjie=$line; $index++; } }}if($content!=''){ $fpw=fopen("../books/$title/".$index.".txt",'w'); $zhangjie.="\r\n"; fwrite($fpw,$zhangjie); fwrite($fpw,$content); fclose($fpw); $sql="select max(idx) from zhengwen where id=$id"; $result=mysql_query($sql); if(!$result) exit; if(mysql_num_rows($result)>=1) { $row=mysql_fetch_row($result); $idx=$row[0]+1; } else { $idx=1; } $sql="insert into zhengwen values('$id','$idx','$type','$zhangjie',now())"; $result=mysql_query($sql); if(!$result) exit;}echo '<br/>'.'data stored sucess!';fclose($fp); 试一试set_time_limit(0); 如果文章太大(5M以上)就有点问题了=====================================php内存原因,优化你的代码,减少每次读取量,不要一次读很多。 估计是这个原因了,memory_limit多少,设置大一些再试试@ini_set('memory_limit', '128M');//供参考 新人提问: linux用哪个的比较好 csdn中下的PHP教学视频用什么放啊?求解! php页面中 分别输出倒数第一,倒数第二个字符 在线等 求助:一个项目经理的职能 帮忙解决下数组的问题!(菜鸟问..) 数据更新的问题 88分求助 我是新手,谁能帮我做个pHP查询数据库的页面啊? 请问如何在php中加入css和javascript 求解一个thinkphp5.1中的两个函数传值问题 帮我看下这个php小程序 利用snoopy采集,如何防止数据重复?
flock($fp,1);
$content='';
$zhangjie='';
$mode="/第.*章/"; //章$index=0;
while(!feof($fp))
{
if($index>0)
{
$line=fgets($fp); //获得行
$temp=trim($line); //去掉首尾空格
if(preg_match($mode,$temp))
{
//写入磁盘目录里面
$fpw=fopen("../books/$title/".$index.".txt",'w');
$line.="\r\n";
fwrite($fpw,$line);
fwrite($fpw,$content);
fclose($fpw);
//写入数据库
$sql="select max(idx) from zhengwen where id=$id";
$result=mysql_query($sql);
if(!$result)
exit;
if(mysql_num_rows($result)>=1)
{
$row=mysql_fetch_row($result);
$idx=$row[0]+1;
}
else
{
$idx=1;
}
$sql="insert into zhengwen values('$id','$idx','$type',$zhangjie',now())";
$result=mysql_query($sql);
if(!$result)
exit; $zhangjie=$temp;
$content='';
$index++; }
else
{
$content.=$line;
}
}
else
{
$line=fgets($fp);
$line=trim($line);
if(preg_match($mode,$line))
{ $zhangjie=$line;
$index++;
}
}}if($content!='')
{
$fpw=fopen("../books/$title/".$index.".txt",'w');
$zhangjie.="\r\n";
fwrite($fpw,$zhangjie);
fwrite($fpw,$content);
fclose($fpw); $sql="select max(idx) from zhengwen where id=$id";
$result=mysql_query($sql);
if(!$result)
exit;
if(mysql_num_rows($result)>=1)
{
$row=mysql_fetch_row($result);
$idx=$row[0]+1;
}
else
{
$idx=1;
}
$sql="insert into zhengwen values('$id','$idx','$type','$zhangjie',now())";
$result=mysql_query($sql);
if(!$result)
exit;
}echo '<br/>'.'data stored sucess!';
fclose($fp);
set_time_limit(0);
估计是这个原因了,memory_limit多少,设置大一些再试试@ini_set('memory_limit', '128M');//供参考