Working with PHP Files and Directories

File Access and EOF

PHP offers a number of functions for handling and working with resources. This articles discusses functions dedicated to file manipulation in PHP.

What is a resource?

A resource is simply an entity from which an input or output stream can be initiated. A stream in computing refers to a sequence of data elements made available over time.

In PHP, this is simply a variable which holds reference to an external entity like a file. Note however that not all resources are necessarily files. For example, a network socket is considered a resource. The terms resource handling and file handling are therefore not the same.

The end of file commonly abbreviated as EOF is a condition where no more data can be read from a data source (Wikipedia). When reading a resource, it is important to determine whether this condition has been met. For this reason, it is common to find built-in functions for verifying this in almost all mainstream programming languages. (Book). In PHP, this function is foef(). Its prototype is int feof(resource $handle).


	$resource = fopen("resource.txt","rt",true);

	while (!feof($resource)) {
		echo nl2br(fgets($resource),true);

The example above uses a text file named resource.txt Functions fopen and fclose open and close the text file respectively. Particular attention should be given to the first function, fopen. This function opens the text file and binds it to what’s known a handle (in the example above, it was bound to a variable named $resource). It’s prototype is resource fopen(string $filename, string $mode [, bool $use_include_path = false [,resource $context]])

The first required parameter, $filename, is simply the URL of the resource to be openned. The example above uses a local file but fopen is also capable of opening files via protocols HTTP, HTTPS and FTP.

Dominique A. Mariano<br />
George Leis<br />
Joseph Buarao<br />
Henry Lacson<br />
<br />
<br />
<br />
<br />
<br />
<br />
Last Name

The second parameter $mode determines the level of access available to the opened resource. In the example above, “rt” is provided as an argument. This means that the resource will be opened as “read-only” and in “text-mode”. The table below summarizes possible arguments for $mode, including the position of the file pointer.

Table 1. Argument Values for $mode
Mode Description File Pointer Position
r Read-only. Beginning of the file.
r+ Read and write Beginning of the file.
w Write-only. Deletes all contents before writing. Creates file if it does not exists. Beginning of the file.
w+ Read and write. Deletes all contents before reading and writing. Creates file if it does not exist. Beginning of the file.
a Write-only. Creates file if it does not exist. Also known as “append”. End of file.
a+ Read and write. Creates file if it does not exist. Also known as “appending to the file”. End of file.
b Opens the file in binary mode N/A
t Opens the file in text mode N/A
This values will be discussed later, following a discussion on functions that read and write into a file.

The third paramter $use_include_path specified whether PHP should look up the resource with the directory provided in the include_path configuation directive of PHP’s configuration file. The default argument for this parameter is false. If provided true or a value of 1, PHP will search the include_path directory, which by default is .;C:xamppphpPEAR. This value will have to be set to C:xamppphpPEAR or any existing directory to make it work.

After interacting with a resource via handle, the handle must be destroyed. Function fclose() in the above example simply does this in the above example. Its protototype is boolean fclose(resource $handle). It returns true when handle is destroyed successfully and false otherwise.

Reading from a File

Creating a handle only prepares a file for manipulation. PHP offers a number of functions that allow us to perform the actual manipulations. We’ll first cover several “read” functions before discussing the “write” ones. These “read” functions include: file(), file_get_contents(), fgetcsv(), fgets(), fgetss(), fgetc(), fread() and readfile()

Function file() reads a file into an array, separating each element by the newline character with the newline still attached to the end of each element. Its prototype is array file(string $filename [,int $flags = 0 [,resource $context]])

   echo "<br />";
    [0] => Dominique A. Mariano

    [1] => George Leis

    [2] => Joseph Buarao

    [3] => Henry Lacson

    [4] => 

    [5] => 

    [6] => 

    [7] => 

    [8] => 

    [9] => 

    [10] => Last Name

Notice that file handles do not need to be defined in order to use this function. Optional parameter $flags can take on one of sereral possible values.

Flag Description
FILE_USE_INCLUDE_PATH Search for the file in the directory specified in the include_path of PHP’s configuration file.
FILE_IGNORE_NEW_LINES Do not add new lines at the end of each array element.
FILE_SKIP_EMPTY_LINES Skip empty lines.

There’s a little caveat about the FILE_SKIP_EMPTY_LINES argument: It doesn’t work seem to work as expected. Consider the demonstration below where the last loop still outputs empty lines.

It appears that we will need to use a trim() function or any other applicable methods to remove the remove the empty lines from the array.

   foreach(file("resource.txt", FILE_USE_INCLUDE_PATH) as $key => $value) {
      echo nl2br($value);
   echo "<hr />";

   foreach(file("resource.txt", FILE_IGNORE_NEW_LINES) as $key => $value) {
       echo nl2br($value);
   echo "<hr />";

   foreach(file("resource.txt", FILE_SKIP_EMPTY_LINES) as $key => $value) {
       echo nl2br($value);
   echo "<hr />";

Function file_get_contents() reads the contents of a file into a string. It’s prototype is file_get_contents(string $filename [,int $flags = 0 [,resource $context [,int $offset = -1, [, int $maxlen = -1]]]]). Parameters $flags and $context operate are the same as in function file().

   $string = file_get_contents("resource.txt");
   echo $string;
Dominique A. Mariano
George Leis
Joseph Buarao
Henry Lacson

Last Name

Note that this function will also read new line characters. To avoid new-line characters altogether, you may want to use function fread(). It’s prototype is string fread(resource $handle, int $length). This function has two required parameters. The first is a file pointer to the resource, the second being the number of characters to be read.

Another similar function is readfile(). Gives us us a bit of an information about the input since it returns the number of bytes read. It’s prototype is int readfile(string $filename [,boll $use_include_path = false [,resource $context]]).

Another way to read files into a sting is  fgetcsv()which parses each line of a file marked up in CSV format. Its prototype is array fgetcsv(resource handle [,int $length [,string $delimiter [, string $enclosure [, string $escape]]]]). Unlike the preceeding example, the first argument must be a handle, not a string. Furthermore, unlike with the previous functions discussed, reading each line does not “stop” at the newline character; rather, it stops and resumes to the next line only after $length characters have been read. This is the purpose of the optional parameter $length. Setting this parameter to 0 or not providing any value means that each line will have an “unlimited” length, which can in turn slow down execution. It is a good practice to set this argument to a number greater than the longest line (in characters) to be found in the file.

The optional $delimiter parameter specifies the character (default is a comma) used to delimit each field in the file while the optional $enclosure parameter identifies the character (default is a double quote) used to enclose the field values. Enclosing field values within enclosures (e.g., double quotes) is helpful when the $delimiter character also appears within the field values.


   $handle = fopen("csv.txt","rt", false);
   while (!feof($handle)) {
       list($name, $email, $age) = fgetcsv($handle, 1024,",");
       echo nl2br(sprintf("Name: %snEmai: %snAge: %snn",$name, $email, $age));

Name: Dominique A. Mariano<br />
Emai: [email protected]<br />
Age: 23<br />
<br />
Name: George Leis<br />
Emai: [email protected]<br />
Age: 20<br />
<br />
Name: Joseph Buarao<br />
Emai: [email protected]<br />
Age: 22<br />
<br />
Name: Henry Lacson<br />
Emai: [email protected]<br />
Age: 27<br />
<br />
Name: <br />
Emai: <br />
Age: <br />
<br />

The $length argument specifies the maximum number of characters in a line that will be read by the previous function discussed. There may be situations where we want to return pnly a specified number of characters from a file. Function fgets() does this. Its prototype is string fgets(resource $handle [, int $length]). The value optional parameter is 1024 characters by default.

    $dna_translation = fopen("dna.txt", "rt");
    $counter = 0;

    echo "<table>";
    while(!feof($dna_translation)) {
    if ($counter == 0) {
        echo "<tr>";

    echo "<td><code>" . fgets($dna_translation,11) . "</code></td>";

    if ($counter > 5) {
	echo " </tr> ";
	$counter = 0;

echo "</table>";



Function fgetss behaves similarly like fgets, except that it strips HTML and PHP tags from intput. Its prototype is string fgetss(resource $handle, int $length [, string $allowable_tags).

It is also possible to get one character at a time from a file. This is accomplished by fgetc(). This returns false if EOF is encoutered. Its prototype is string fgetc(resource $handle).

Another convenient function to have at our disposal is fscanf(). It parses a result based on a predefined format. Its prototype is fscanf(resource $handle, string $format [, mixed&$...]). The example and output below briefly explains what this function does.

   $fh = fopen("sss.txt","rt");

   while(!feof($fh)) {
      list($p1, $p2, $p3) = fscanf($fh,"%d-%d-%d");
      echo "<br />n";

321*321*654<br />
654*654*654<br />
111*666*988<br />
135*689*114<br />
329*549*321<br />

Leave a Reply

Your email address will not be published. Required fields are marked *