note 46407 added to function.fseek

From: Date: Sun, 10 Oct 2004 02:48:30 +0000
Subject: note 46407 added to function.fseek
Groups: php.notes 
Request: Send a blank email to php-notes+get-78258@lists.php.net to get a copy of this message
I wrote a function for read lines from file in reverse.May be we usual need to view logs of website.but we dont want to open whole file,it is too slower to open a large file.We need only to view lastest few lines.This is the reason for me to wrote a function do it.I hope it will useful to you. /* * @author :zolo * @create date:2008/09/11 * @params * $filename (in param: the log file path name for read) * $readlines(in param: lines to be read) * $order (in param: order direction,ESC or DESC) * @return * return a HTML style string,it package each line with <td> and </td>. * @notices * in this function,it out put as HTML style.pls look at the sample under. */ function readlastline($filename,$readlines,$order) { $filelen=filesize($filename); $readedlines=0; $pos=-1; $fp=@fopen($filename,"r"); $char=" "; $result=""; $temp=""; $newline=false; while((abs($pos)<$filelen)&& ($readedlines<$readlines)) { $newline=false; fseek($fp,$pos,SEEK_END); $char=fgetc($fp); if($char=="\r" || $char=="\n") { $stemp="<tr bgcolor=\"#CCCCCC\"><td>"; $stemp.=$temp; $stemp.="<td></tr>"; if($order=="ESC") { $result=$stemp.$result; } else { $result.=$stemp; } $readedlines++; $newline=true; $temp=""; $pos--; if(abs($pos)<$filelen) { fseek($fp,$pos,SEEK_END); $char=fgetc($fp); while($char=="\r"|| $char=="\n") { $pos--; fseek($fp,$pos,SEEK_END); $char=fgetc($fp); } } } else { $temp=$char.$temp; $pos--; } } fclose($fp); return $result; } ///*************************************** example: echo ("<table width=\"100%\" align=\"center\">"); echo readlastline("log.file",8,"DESC"); echo ("</table>"); ---- Manual Page -- http://www.php.net/manual/en/function.fseek.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46407 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46407&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46407&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78258) next »