w1ww Established Member ★ 15 ★ Impact 9 Jan 19, 2007 894 views 9 replies #1 Hello, Using the file read function PHP: $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); How can I make it to just read the last 5 lines of the file? Thank you
Hello, Using the file read function PHP: $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); How can I make it to just read the last 5 lines of the file? Thank you
L lee101 Established Member ★ 15 ★ Impact 9 Jan 19, 2007 #2 PHP: $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: $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
ahtum VIP Member VIP ★ 20 ★ Impact 74 Jan 19, 2007 #3 lee101 said: PHP: $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 Click to expand... PHP: $file = file('file.txt'); foreach($file as $line){ echo $line; } easier :p
lee101 said: PHP: $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 Click to expand... PHP: $file = file('file.txt'); foreach($file as $line){ echo $line; } easier :p
L lee101 Established Member ★ 15 ★ Impact 9 Jan 19, 2007 #4 ahtum said: easier :p Click to expand... Yes, but w1ww only wants the last 5 lines, that would display all of them
ahtum said: easier :p Click to expand... Yes, but w1ww only wants the last 5 lines, that would display all of them
w1ww Established Member ★ 15 ★ Impact 9 Jan 19, 2007 #5 ahtum, Thanks for your input but lee101 solution works better, although, using his code I get the text, but I get also: PHP: Fatal error: Maximum execution time of 30 seconds exceeded in 3.php on line 7 what is wrong? Thank you
ahtum, Thanks for your input but lee101 solution works better, although, using his code I get the text, but I get also: PHP: Fatal error: Maximum execution time of 30 seconds exceeded in 3.php on line 7 what is wrong? Thank you
L lee101 Established Member ★ 15 ★ Impact 9 Jan 19, 2007 #6 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: $file=file('testFile.php'); $lines=count($file); $i=$lines-5; while($i < $lines){ echo $file[$i]; $i++; }
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: $file=file('testFile.php'); $lines=count($file); $i=$lines-5; while($i < $lines){ echo $file[$i]; $i++; }
Matthew. VIP Member VIP ★ 15 ★ Impact 89 Jan 19, 2007 #8 Topic is solved but just my input: PHP: $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 :santa: Last edited: Jan 19, 2007
Topic is solved but just my input: PHP: $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 :santa:
Dan Buy my domains.VIP Member VIP ★ 15 ★ Impact 108 Jan 19, 2007 1 point #9 I know it's solved, too, but: Matthew, that would show the lines backwards. (I think.) PHP: $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
I know it's solved, too, but: Matthew, that would show the lines backwards. (I think.) PHP: $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
Matthew. VIP Member VIP ★ 15 ★ Impact 89 Jan 19, 2007 #10 Ahh good point Dan. That didn't even enter my head (p.s. Your example is correct)