I mentioned FileOpen() because, in PHP, you need to open the file with fopen() (rather than file_get_contents()) to be able to use fseek() or fgets(). I was hoping there was a FileSeek() function in ColdFusion.
You might be able to loop through the file using FileReadLine() to move the file pointer ahead to a specific line, then process only a few lines. The following PHP code demonstrates this strategy.
read_chunk.php
Code: Select all
<?php
$fh = fopen('example_long_file.txt', 'r');
// Line number to start at (first is 0)
$start = 5;
// Number of lines to get
$get = 5;
if ($fh)
{
// Current line number (increments to 0 before first line is read)
$line = -1;
// Loops through every line before line $start + $get
while ($line < $start + $get - 1 && !feof($fh))
{
// Increments the line counter
$line++;
// Gets the current line and moves the pointer ahead
$line_contents = fgets($fh);
if ($line < $start)
{
// Debugging only: Shows the lines that are skipped
echo "<div style='color:#CCC'>$line: $line_contents</div>\n";
// Does not process any line before the start line
continue;
}
// Process the "gotten" lines here
// Debugging only: Shows the lines that are processed
echo "<div style='font-weight:bold'>$line: $line_contents</div>\n";
}
}
?>
example_long_file.txt
[text]THE BROOKLYN BRIDGE
PEAKS AND VALLEYS
CONSTRUCTION FOREMAN
UNSPOILED OCEAN VIEWS
NEW YEAR'S & UNITED NATIONS RESOLUTION
MAKE IT SNAPPY
WOOL CREW NECK SWEATER
PROVIDENCE RHODE ISLAND
HOMEBODY
SLEDS AND TOBOGGANS
POPLAR TREES
JOHANNESBURG SOUTH AFRICA
HOT ARTICHOKE SPINACH DIP
HAVING THE ENTIRE BEACH TO YOURSELF
THE BIGGEST LOSER
NEEDLE-NOSE PLIERS
GIVE ME A HINT
SUGAR COOKIES
INNKEEPER
TRADITIONAL SPANISH OMELETTE
A FIGMENT OF YOUR IMAGINATION
IS THERE A DOCTOR IN THE HOUSE DRESSING
STORAGE UNIT
THE DRYER'S LINT SCREEN
FASHION POLICE
BABY'S BREATH
METEOROLOGIST
STATE CAPITAL
WIDE-BRIMMED STRAW HAT
PATRICIA ARQUETTE STARS IN MEDIUM[/text]
Output of read_chunk.php
0: THE BROOKLYN BRIDGE
1: PEAKS AND VALLEYS
2: CONSTRUCTION FOREMAN
3: UNSPOILED OCEAN VIEWS
4: NEW YEAR'S & UNITED NATIONS RESOLUTION
5: MAKE IT SNAPPY
6: WOOL CREW NECK SWEATER
7: PROVIDENCE RHODE ISLAND
8: HOMEBODY
9: SLEDS AND TOBOGGANS
Edit: This post was recovered from search engine cache.