You want to read in only a little of the file at a time or to count the number of lines in a file.
Use fgets(). It returns a string of a certain length. Because it reads a line at a time, you can count the number of times it's called before reaching the end of a file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>Reading Files Incrementally</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if (isset($_POST['submit'])) { process_form(); } else { display_form();// display form for the first time } function display_form() { echo <<<HTML <h2>Reading Files Incrementally</h2> <form action = "$_SERVER[SCRIPT_NAME]" method="post">\n Filename: <input type="text" name="file" size="50" value="http://voyager.deanza.edu$_SERVER[SCRIPT_NAME]" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> HTML; } function process_form() { $lines = 0; $file = "$_POST[file]"; echo "<font color=blue>"; // open the file for reading if ($fh = fopen("$file", 'r')) { // test for end of file on a file pointer with feof() while (! feof($fh)) { // get a line from the file $s = fgets($fh); echo htmlentities($s), "<br />"; if ($s) { $lines++; } } } echo "</font>"; echo "<p>The file $file has $lines lines.</p>\n"; echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n"; echo "<p><a href=\"example_l.php\">Do you want to write to the file?</a></p>\n"; } ?> </p> </body> </html> |