Print this Section


Reading Files

After the file is successfully opened, various functions can be used to read the contents of the file. The following example illustrates how to read the entire contents of a file using the fread() function:


The first step is to create a variable to hold the full path to the file that will be opened for reading:

$filename = "C:/Documents and Settings/Administrators/MyFiles/myfile.txt"; 

The path to the text file, myfile.txt, is stored in the variable called $filename. Next, a file pointer, called $newfile, is created and used with the fopen() function to open the file specified in the previous section. A file pointer is used to refer to the just-opened file:

$newfile = fopen($filename, "r"); 

The file pointer is a PHP variable that contains a reference to the opened file. It will be used later to with the fread() function to read content from the opened file.

Next, a variable called $file_contents is created and used to store the contents of the text file, myfile.txt. The first parameter of the fread() function refers to the name of the file whose contents will be read. The second parameter specifies the length of the file. If the length of the file is unknown, a special PHP function - filesize() can be used. The filesize() function retrieves the entire contents of a file. It requires a single parameter - the name or path of the file currently being read.

$file_contents = fread($newfile, filesize($filename)); 

The entire contents of the text file are now stored in the variable $file_contents. This data can be displayed to the screen using the echo statement or later written to another text file. Assume the contents of "myfile.txt" include:


If we use the echo statement to output the contents of the file, the following is displayed in the browser:

echo $filecontents;

A,B,C,1,2,3, D,E,F,G 8315125 

Notice that the file contents are displayed without the line breaks. When using fread() the entire contents of the file is stored in a single variable, making it difficult to work with individual pieces of the file. Another function used for reading file contents is the fgets() function. The fgets() function is similar to fread() except fgets() returns only the first line of the file.

In order for the file to read multiple lines, a while loop can be used to scan the file until the end of file is reached. The feof() function is used to check for the end of the file.


$filename = "C:/Documents and Settings/Administrator/MyFiles/myfile.txt";

$newfile = @fopen($filename, "r") or exit("Could not open file");


echo fgets($newfile) . "<br>";




In some cases, it may be necessary to read and work with individals parts of the text file content. If the text file contains delimiters to separate individual pieces of data, an alternative read function, fgetcsv(), can be used instead. This function reads the file contents and creates an array making specific parts of the text accessible.

Assume the text file, numbers.txt, exists and contains the following data:



The following script demonstrates the use of the fgetcsv() function to read the contents of the text file.



$filename = "C:/numbers.txt";

$newfile = @fopen($filename, "r") or exit("Could not open file");

$file_contents = @fgetcsv($newfile, filesize($filename),",") or exit("Could not read file contents");

foreach ($file_contents)

        echo $file_contents[$i];
echo "<br>";




After opening the file, the fgetcsv() reads the entire contents of the file creating an array - '$file_contents'. The third parameter of fgetcsv() function specifies that each element separated by the "," delimiter will become an element of the new array. Since numbers.txt contains the delimited values 50,28,34,90, $file_contents[0] = 50, $file_contents[1] = 28, $file_contents[2] = 34, $file_contents[3] = 90. Once the array is created, the values can be manipulated using any of the PHP array functions. In the previous example, a while loop iterates through the $file_contents() array and displays each number. After all file processing is complete, the fclose() function is used to close the open file.

TOP | NEXT: Writing to Files