How to fread() starting from a certain line?

Started on the old forum in February 2013 · view the original in the archive · old links redirect here

#fgets #fread #mysql #text

3 replies · 641 views · started 13 years ago · 1 min read

4 participants
s3nzo Developer · 156 posts #1

Hello Guys.

I get a lot of server hangs and exceed max_connections, so i wanted to try reducing the load by using a .txt file. Do you know if it's possible to read a file starting from a certain line? e.g if i want to read a file starting from line 10 ending at line 15? on a db it's

$mysqli->query("SELECT * FROM `table` LIMIT 10, 5")

Now can the same results be achieved reading a txt file?

ionutvmi Junior · 45 posts #2

maybe like this

function myfread($file, $start = 1, $end = false){
	$content = '';
	$data = file($file);
	for($i = ($start-1); $i < ($end ? $end : count($data)); $i++)
		$content .= $data[$i];
	return $content;
}

echo myfread("test.txt",2,5)." - ";
echo myfread("test.txt",4)." - ";
echo myfread("test.txt")." - ";

PIMP-WAP.NET - PIMP YOUR MOBILE

MASTER-LAND.NET - WAPMASTERS LAND

kevk3v Senior · 332 posts #3
<?php

$f = fopen('myfile.txt', 'r');
$lineNo = 0;
$startLine = 3;
$endLine = 6;
while ($line = fgets($f)) {
    $lineNo++;
    if ($lineNo >= $startLine) {
        echo $line;
    }
    if ($lineNo == $endLine) {
        break;
    }
}
fclose($f);

?>

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

shushant Developer · 207 posts #4

why dont you use SQLite or Doctrine 2

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close