一个分页读取TXT 文件的阅读程序
主要内容来自网上 ,我做了一些修改 加上了读取数据库的功能。
运行下来这段代码读取大文件的时候速度太慢,现在改进了一下 回头重发一个贴。
<?php session_start(); header("Content-Type:text/html;charset=utf-8"); include_once("conMySQL.php"); if (empty($page)) {$page=1;} if (isset($_GET['page'])==TRUE) {$page=$_GET['page']; } ?> <?php if($page){ $SQL="select idd,id,lx,mc,dx,rq from book where idd=".$_GET["idd"]; //echo $SQL,"<br>"; $r=mysqli_query($cn,$SQL); if ($r->num_rows > 0) { $s=mysqli_fetch_array($r); $SQL=$s["id"]; //echo $SQL. "<br>"; $filename=substr($SQL,3); for($i=strlen($filename);$i<10;$i++){ $filename="0".$filename; } $filename="txt/". substr($filename,4,3 ). "/". $filename.".txt"; //echo $filename; $SQL=""; $SQL= $SQL. "<th>书名:".$s["mc"]. " </th>"; $SQL= $SQL. "<td>类型:".$s["lx"]. " 文件大小:".$s["dx"]. "kb "; $SQL= $SQL. " <a href='download.php?idd=". $_GET["idd"]. "'>下载全本</a>"; $SQL= $SQL. "</td>"; } $counter=file_get_contents($filename); //读取txt文件内容到$counter $length=strlen($counter); $page_count=ceil($length/5000); function msubstr($str,$start,$len){ $strlength=$start+$len; $tmpstr=""; for($i=0;$i<$strlength;$i++) { if(ord(substr($str,$i,1))==0x0a) { $tmpstr.='<br />'; } if(ord(substr($str,$i,1))>0xa0) { $tmpstr.=substr($str,$i,2); $i++; } else{ $tmpstr.=substr($str,$i,1); } } $tmpstr=strToUtf8($tmpstr); return $tmpstr; } function strToUtf8($str){ $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); if($encode == 'UTF-8'){ return $str; }else{ return mb_convert_encoding($str, 'UTF-8', $encode); } } //------------截取中文字符串--------- $c=msubstr($counter,0,($page-1)*5000); $c1=msubstr($counter,0,$page*5000); }?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $s["mc"]; ?></title> <style type="text/css"> <!-- .STYLE1 {font-size: 12px} .STYLE2 {font-size: 18px} --> </style> </head> <body> <table width="100%" bgcolor="#cccccc"> <tr> <?php echo $SQL; ?> </tr> <tr> <td width="42%" align="center" valign="middle"><span class="STYLE1"> <?php echo $page;?> / <?php echo $page_count;?> 页 </span></td> <td width="58%" height="28" align="left" valign="middle"> <span class="STYLE1"> <?php echo "<a href=disp.php?page=1&idd=".$_GET["idd"].">首页</a> "; if($page!=1){ echo "<a href=disp.php?page=".($page-1)."&idd=".$_GET["idd"].">上一页</a> "; } if($page<$page_count){ echo "<a href=disp.php?page=".($page+1)."&idd=".$_GET["idd"].">下一页</a> "; } echo "<a href=disp.php?page=".$page_count."&idd=".$_GET["idd"].">尾页</a> "; ?> </span> </td> </tr> </table> <table width="100%" bgcolor="#CCCCCC"> <tr> <td > <?php echo substr($c1,strlen($c),strlen($c1)-strlen($c)); ?> </td> </tr> </table> <table width="100%" bgcolor="#cccccc"> <tr> <td width="42%" align="center" valign="middle"><span class="STYLE1"> <?php echo $page;?> / <?php echo $page_count;?> 页 </span></td> <td width="58%" height="28" align="left" valign="middle"> <span class="STYLE1"> <?php echo "<a href=disp.php?page=1&idd=".$_GET["idd"].">首页</a> "; if($page!=1){ echo "<a href=disp.php?page=".($page-1)."&idd=".$_GET["idd"].">上一页</a> "; } if($page<$page_count){ echo "<a href=disp.php?page=".($page+1)."&idd=".$_GET["idd"].">下一页</a> "; } echo "<a href=disp.php?page=".$page_count."&idd=".$_GET["idd"].">尾页</a> "; ?> </span> </td> </tr> </table> </body>
发表评论