i have a text file include two lines line 1 line 2 i know how to read the first line with this code <?php $myFile = "test.txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; ?> but i canot read the second line please help me on this
try this $filename = "file.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $data = explode("\n",$contents); for($i=0,$b=sizeof($data);$i<$b;$i++) { print $data[$i]; } PHP: $data becomes an array where each piece is a line of the text file. so there there are 20 lines in the text file, the array will have 20 pieces in it.
Use fgets again. Each time you use that it will read the next line in the file. Unless you know the position of the \n in the file, fgets is the easiest method to get a specific line.
<?php $myFile = "test.txt"; $fh = fopen($myFile, 'r'); $line1 = fgets($fh); $line2 = fgets($fh); fclose($fh); echo "Line 1: $line1"; echo "Line 2: $line2"; ?> PHP:
yes it works thank you but i want to take the first line but the value on another variable how i do this?