ʹÓÃphp¶ÁÈ¡²Ù×÷´óÎļþ·½·¨»ã×ܼ°ÓÅ»¯
1. Ö±½Ó²ÉÓÃfileº¯ÊýÀ´²Ù×÷
×¢£ºÓÉÓÚ fileº¯ÊýÊÇÒ»´ÎÐÔ½«ËùÓÐÄÚÈݶÁÈëÄڴ棬¶øphpΪÁË·ÀֹһЩдµÄ±È½ÏÔã¸âµÄ³ÌÐòÕ¼ÓÃÌ«¶àµÄÄÚ´æ¶øµ¼ÖÂϵͳÄÚ´æ²»×㣬ʹ·þÎñÆ÷³öÏÖå´»ú£¬ËùÒÔĬÈÏÇé¿öÏ ÏÞÖÆÖ»ÄÜ×î´óʹÓÃÄÚ´æ16M,ÕâÊÇͨ¹ýphp.iniÀïµÄ memory_limit = 16MÀ´½øÐÐÉèÖã¬Õâ¸öÖµÈç¹ûÉèÖÃ-1£¬ÔòÄÚ´æʹÓÃÁ¿²»ÊÜÏÞÖÆ.
ÏÂÃæÊÇÒ»¶ÎÓÃfileÀ´È¡³öÕâ¾ßÎļþ×îºóÒ»ÐеĴúÂ룺
<?php ini_set('memory_limit','-1'); $file = 'access.log'; $data = file($file); $line = $data[count($data)-1]; echo $line; ?>Õû¸ö´úÂëÖ´ÐÐÍê³ÉºÄʱ 116.9613 (s).
ÎÒ»úÆ÷ÊÇ2¸öGµÄÄڴ棬µ±°´ÏÂF5ÔËÐÐʱ£¬ÏµÍ³Ö±½Ó±ä»Ò£¬²î²»¶à20·ÖÖÓºó²Å»Ö¸´¹ýÀ´£¬¿É¼û½«Õâô´óµÄÎļþÈ«²¿Ö±½Ó¶ÁÈëÄڴ棬ºó¹ûÊǶàÉÙÑÏÖØ£¬ËùÒÔ²»ÔÚÍò ²»µÃÒÔ£¬memory_limitÕⶫÎ÷²»Äܵ÷µÃÌ«¸ß£¬·ñÔòÖ»Óдòµç»°¸ø»ú·¿£¬ÈÃreset»úÆ÷ÁË.
2.Ö±½Óµ÷ÓÃlinuxµÄtailÃüÁîÀ´ÏÔʾ×î ºó¼¸ÐÐ
ÔÚlinuxÃüÁîÐÐÏÂ,¿ÉÒÔÖ±½ÓʹÓÃtail -n 10 access.logºÜÇáÒ×µÄÏÔʾÈÕÖ¾Îļþ×îºó¼¸ÐÐ,¿ÉÒÔÖ±½ÓÓÃphpÀ´µ÷ÓÃtailÃüÁî,Ö´ÐÐphp´úÂëÈçÏÂ.
file = 'access.log'; $file = escapeshellarg($file); // ¶ÔÃüÁîÐвÎÊý½øÐа²È«×ªÒå $line = `tail -n 1 $file`; echo $line;Õû¸ö´úÂëÖ´ÐÐÍê³ÉºÄʱ 0.0034 (s)
3. Ö±½ÓʹÓÃphpµÄfseekÀ´½øÐÐÎļþ²Ù×÷
ÕâÖÖ·½Ê½ÊÇ×îΪÆÕ±éµÄ·½Ê½,Ëü²»ÐèÒª½«ÎļþµÄÄÚÈÝÈ«²¿¶ÁÈëÄÚÈÝ,¶øÊÇÖ±½Óͨ¹ýÖ¸ÕëÀ´²Ù×÷,ËùÒÔЧÂÊÊÇÏ൱¸ßЧµÄ.ÔÚʹÓÃfseekÀ´¶ÔÎļþ½øÐвÙ×÷ʱ,Ò²ÓжàÖÖ²»Í¬µÄ·½·¨,ЧÂÊ¿ÉÄÜÒ²ÊÇÂÔÓвî±ðµÄ,ÏÂÃæÊdz£ÓõÄÁ½ÖÖ·½·¨.
·½·¨Ò»
Ê×ÏÈͨ¹ýfseekÕÒµ½ÎļþµÄ×îºóһλEOF£¬È»ºóÕÒ×îºóÒ»ÐеÄÆðʼλÖã¬È¡ÕâÒ»ÐеÄÊý¾Ý£¬ÔÙÕÒ´ÎÒ»ÐеÄÆðʼλÖ㬠ÔÙÈ¡ÕâÒ»ÐеÄλÖã¬ÒÀ´ÎÀàÍÆ£¬Ö±µ½ÕÒµ½ÁË$numÐС£ÊµÏÖ´úÂëÈçÏÂ:
<?php $fp = fopen($file, "r"); $line = 10; $pos = -2; $t = " "; $data = ""; while ($line > 0) { while ($t != "\n") { fseek($fp, $pos, SEEK_END); $t = fgetc($fp); $pos --; } $t = " "; $data .= fgets($fp); $line --; } fclose ($fp); echo $data ?>Õû¸ö´úÂëÖ´ÐÐÍê³ÉºÄʱ 0.0095 (s)
·½·¨¶þ
ÏÖÔÚÎÒÃǼÌÐøÓÅ»¯fseek·½·¨£¬»¹ÊDzÉÓÃfseekµÄ·½Ê½´ÓÎļþ×îºó¿ªÊ¼¶Á,µ«Õâʱ²»ÊÇһλһλµÄ¶Á,¶øÊÇÒ»¿éÒ»¿éµÄ¶Á,ÿ¶ÁÒ»¿éÊý¾Ýʱ,¾Í½«¶ÁÈ¡ºóµÄÊý¾Ý·ÅÔÚÒ»¸öbufÀï,È»ºóͨ¹ý»»Ðзû(\n)µÄ¸öÊýÀ´ÅжÏÊÇ·ñÒѾ¶ÁÍê×îºó$numÐÐÊý¾Ý.ʵÏÖ´úÂëÈçÏ£º
<?php $fp = fopen($file, "r"); $num = 10; $chunk = 4096; $fs = sprintf("%u", filesize($file)); $max = (intval($fs) == PHP_INT_MAX) ? PHP_INT_MAX : filesize($file); for ($len = 0; $len < $max; $len += $chunk) { $seekSize = ($max - $len > $chunk) ? $chunk : $max - $len; fseek($fp, ($len + $seekSize) * -1, SEEK_END); $readData = fread($fp, $seekSize) . $readData; if (substr_count($readData, "\n") >= $num + 1) { preg_match("!(.*?\n){".($num)."}$!", $readData, $match); $data = $match[0]; break; } } fclose($fp); echo $data; ?>Õû¸ö´úÂëÖ´ÐÐÍê³ÉºÄʱ 0.0009(s).
ÊDz»ÊÇЧÂÊÌá¸ßÁ˺ܶ࣬ÏÖÔÚ»¹Äܲ»ÄÜÓÅ»¯ÄØ£¿Çë¿´µÚÈýÖÖ·½·¨¡£
·½·¨Èý
·Ï»°ÉÙ˵£¬Ö±½ÓÉÏ´úÂëÈçÏ£º
<?php function tail($fp,$n,$base=5) { assert($n>0); $pos = $n+1; $lines = array(); while(count($lines)< =$n){ try{ fseek($fp,-$pos,SEEK_END); } catch (Exception $e){ fseek(0); break; } $pos *= $base; while(!feof($fp)){ array_unshift($lines,fgets($fp)); } } return array_slice($lines,0,$n); } var_dump(tail(fopen("access.log","r+"),10)); ?>Õû¸ö´úÂëÖ´ÐÐÍê³ÉºÄʱ 0.0003(s).
ÊDz»ÊÇЧÂÊ´ó´óÌá¸ßÁË£¬Ï£Íû±¾ÎĶÔÄãÓÐËù°ïÖú¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ