我用PHP写了个页面,用来把一本小说(txt格式)根据文章分成一篇一篇,然后分别把它们写入MYSQL数据库,我调试了一下代码没有错,如果文章比较小的话可以成功运行,但是如果文章太大(5M以上)就有点问题了,大家可以看一下这该怎么解决?

解决方案 »

  1.   

    $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);
      

  2.   

    试一试
    set_time_limit(0); 
      

  3.   

    如果文章太大(5M以上)就有点问题了=====================================php内存原因,优化你的代码,减少每次读取量,不要一次读很多。
      

  4.   


    估计是这个原因了,memory_limit多少,设置大一些再试试@ini_set('memory_limit',          '128M');//供参考