Results from the most recent live auction are here .
22 members in the live chat room. Join Chat !
01-19-2007, 05:21 AM
· #1 NamePros Regular
Name: Tiago
Location: Mars.
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (
Donate )
File Read [PHP]
Hello,
Using the file read function
PHP Code:
$myFile = "testFile.txt" ;
$fh = fopen ( $myFile , 'r' );
How can I make it to just read the last 5 lines of the file?
Thank you
__________________
01-19-2007, 05:44 AM
· #2 NamePros Regular
Name: Lee
Location: United Kingdom
Join Date: Mar 2006
PHP Code:
$file = file ( 'testFile.php' );
$lines = count ( $file );
$i = $lines - 5 ;
while( $i < $file ){
echo $file [ $i ];
$i ++;
}
I think that may work, it isn't checked at all though, so there may be errors
01-19-2007, 05:48 AM
· #3 DNOA Member
Name: Vlad
Join Date: May 2006
Posts: 1,267
NP$: 1620.58 (
Donate )
Originally Posted by lee101 PHP Code:
$file = file ( 'testFile.php' );
$lines = count ( $file );
$i = $lines - 5 ;
while( $i < $file ){
echo $file [ $i ];
$i ++;
}
I think that may work, it isn't checked at all though, so there may be errors
PHP Code:
$file = file ( 'file.txt' );
foreach( $file as $line ){
echo $line ;
}
easier
01-19-2007, 06:03 AM
· #4 NamePros Regular
Name: Lee
Location: United Kingdom
Join Date: Mar 2006
Originally Posted by ahtum
easier
Yes, but w1ww only wants the last 5 lines, that would display all of them
01-19-2007, 06:05 AM
· #5 NamePros Regular
Name: Tiago
Location: Mars.
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (
Donate )
ahtum,
Thanks for your input but lee101 solution works better, although, using his code I get the text, but I get also:
PHP Code:
Fatal error : Maximum execution time of 30 seconds exceeded in 3.php on line 7
what is wrong?
Thank you
__________________
01-19-2007, 06:08 AM
· #6 NamePros Regular
Name: Lee
Location: United Kingdom
Join Date: Mar 2006
what are lines 6-8 on 3.php, it sounds like another problem
edit:sorry, i'd wrote the code wrong, use this instead:
PHP Code:
$file = file ( 'testFile.php' );
$lines = count ( $file );
$i = $lines - 5 ;
while( $i < $lines ){
echo $file [ $i ];
$i ++;
}
01-19-2007, 06:10 AM
· #7 NamePros Regular
Name: Tiago
Location: Mars.
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (
Donate )
It works now
Thank you guys!!
__________________
01-19-2007, 07:07 AM
· #8 Stud Sausage
Location: England
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (
Donate )
Topic is solved but just my input:
PHP Code:
$file = file ( 'testFile.php' );
for( $i = 0 ; $i <= 5 ; $i ++)
{
echo $file [ count ( $file )- $i ];
}
Just a little cleaner than using while
edit: Read below
Last edited by Matthew. : 01-19-2007 at 02:20 PM .
01-19-2007, 02:07 PM
· #9 Buy my domains.
Name: Dan
Join Date: Feb 2006
Posts: 2,801
NP$: 54.00 (
Donate )
I know it's solved, too, but:
Matthew, that would show the lines backwards. (I think.)
PHP Code:
$file = file ( 'testFile.php' );
for ( $i = 5 ; $i >= 0 ; $i --) {
echo $file [ count ( $file )- $i ];
}
I don't know if that would work because count returns the number of items but that is one higher than the arrays highest key (if you can understand what I'm trying to say.) You might have to start with $i as 6 and just do $i > 0; :x
01-19-2007, 02:18 PM
· #10 Stud Sausage
Location: England
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (
Donate )
Ahh good point Dan. That didn't even enter my head
(p.s. Your example is correct)
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off