PHP: Read Specific Line From File

Sang Froid picture Sang Froid · Apr 25, 2011 · Viewed 99.4k times · Source

I'm trying to read a specific line from a text file using php. Here's the text file:

foo  
foo2

How would I get the content of the second line using php? This returns the first line:

<?php 
$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>

..but I need the second.

Any help would be greatly appreciated

Answer

user557846 picture user557846 · Apr 25, 2011
$myFile = "4-24-11.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2

file — Reads entire file into an array