Reading Files

This section describes how to use PHP to read file contents on Windows systems.

PHP includes the fread() and filesize() functions for reading files. The functions are defined below:

fread(resource handle, length) - function used to read the contents of a file. Reads up to length bytes from the file pointer referenced by handle. Reading stops when length bytes have been read, EOF (end of file) is reached. The function requires two parameters - a file pointer that is created when the file is opened with fopen() and a filesize that specifies how much of the file contents to read.

fgetcsv(resource handle, length, delimiter) - function used to read the contents of a file and parses the data to create an array. Data is parsed on the delimiter parameter supplied to the function.

filesize(filename) - returns the size of a file. If an error occurs the function returns a false value.

The following example illustrates how to read the entire contents of a file:

fileread.php

<?php

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

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

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

fclose($newfile);

?>

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.

In some cases, it may be necessary to read and work with individals parts of the text file content. 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. 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:

numbers.txt

50,17,34,90

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

fileread.php

<?php

$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");

while ($i=0; $i < sizeof($file_contents); $i++)
{

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

}

fclose($newfile);

?>

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.