kalabox Posted February 28, 2013 Report Share Posted February 28, 2013 kā dabūt lai nolasa tikai pēdējos 5 ierakstus?? <?php $file = "viesi.txt"; if (fopen($file, "r")) { $fil = "viesi.txt"; $fo = fopen ($fil, "r"); $con = fread ($fo,filesize ($fil)); fclose ($fo); echo "$con"; } else { echo "<h3>Radās kļūda, sazinies ar mums<h3>"; } ?> Quote Link to comment Share on other sites More sharing options...
404 Posted February 28, 2013 Report Share Posted February 28, 2013 (edited) Izmanto datu bāzi failu vietā. Bet ja gribas caur pakaļu,tad var ielasīt visu failu masīvā,apmest reversā un ciklā izvadīt cik vajag. Edited February 28, 2013 by 404 Quote Link to comment Share on other sites More sharing options...
Kavacky Posted February 28, 2013 Report Share Posted February 28, 2013 Kāpēc kaut kas jāmet riņķī? $f = file('fails.txt'); for ($i = 0; $i < 5; $i++) { echo array_pop($f); } Quote Link to comment Share on other sites More sharing options...
404 Posted February 28, 2013 Report Share Posted February 28, 2013 Uzvedināja uz V 0.2 beta: $file = file('viesi.txt'); echo implode('<br/>', array_slice(array_reverse($file), 0, 5)); Quote Link to comment Share on other sites More sharing options...
Kasspars Posted February 28, 2013 Report Share Posted February 28, 2013 (edited) Izmanto datu bāzi failu vietā. Bet ja gribas caur pakaļu,tad var ielasīt visu failu masīvā,apmest reversā un ciklā izvadīt cik vajag. Un ja fails ir 20gb liels, tad arī lasīsi masīvā? Ja ieraksti atdalās ar newline, tad ar fseek aizej līdz pašām faila beigām un tad ciklā ar fread lasi pa vienam simbolam. Cheko katru simbolu. Ciklu pārtrauc kad esi atradis 5 newline simbolus $f = fopen( 'fails.txt', 'r' ); $pos = filesize( 'fails.txt' ) - 1; $found = 0; $lastLines = ''; fseek( $f, $pos ); while ( $s = fread( $f, 1 ) ) { if ( $s == "\n" ) if ( ++$found > 4 ) break; $lastLines = $s.$lastLines; fseek( $f, --$pos ); } var_dump( explode( "\n", $lastLines ) ); Edited February 28, 2013 by aaxc Quote Link to comment Share on other sites More sharing options...
404 Posted February 28, 2013 Report Share Posted February 28, 2013 Un ja fails ir 20gb liels, tad arī lasīsi masīvā?Nē-pie tāda viesu grāmatas trafika lasīšu memkešā no datu bāzes. Bet ja saki,ka šādas tēmas autoram fseek ir tas kas vajadzīgs,tad steidzami labojos un drošu sirdi rekomendēju pielāgot pēc vajadzībām šo: http://stackoverflow.com/questions/2961618/how-to-read-only-5-last-line-of-the-txt-file/#answer-13936967 Quote Link to comment Share on other sites More sharing options...
blackhalt Posted March 2, 2013 Report Share Posted March 2, 2013 Visādi var :) <?php system("tail --lines=5 viesi.txt"); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.