1. Home
  2. Computing & Technology
  3. PHP / MySQL

File () PHP Function

By , About.com Guide

Definition: The file () PHP function retrieves data from a file and puts it into an array. The array consists of the line number, and the data held in that line starting with 0. If your data isn't separated by new lines this may not return what you expect. If you are running an HTML file through file (), you should also use htmlspecialchars. If you want to return the data to a string instead of an array, you should use file_get_contents.
Examples:
<?php
$lines = file('YourFile.txt');
foreach ($lines as $line_num => $line)
{
print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n";
//If you are reading HTML code use this line instead
//print "<font color=red>Line #{$line_num}</font> : " . htmlspecialchars($line) . "<br />\n";
}
?>
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. File PHP - Open PHP File - Include File in PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.