Jump to content
php.lv forumi

lai nolasa pēdējos 5 ierakstus


kalabox

Recommended Posts

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>";
}
?>
Link to comment
Share on other sites

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 by aaxc
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...