Opening and Closing Files

   

Opening Files

Before you can use PHP to read a file, you must first open it. To open a file, you use the fopen() function.

 $file_handler = fopen($file, $mode)  

You must assign the function to a variable, as above. This is referred to as a file pointer. You use the file pointer to reference the open file throughout your script.

The fopen() function takes two arguments:

  • $file is the name (and path, if required) of the file.

  • $mode is one of a list of available modes:

    • r Open the file as read-only. You cannot write anything to the file. Reading begins at the beginning of the file.

    • r+ Open the file for reading and writing. Reading and writing begin at the beginning of the file. You will delete existing contents if you write to the file without first moving the internal pointer to the end of the file.

    • w Open the file for write-only. You cannot read data from the file. Writing begins at the beginning of the file. You will again delete contents if you write to the file without first moving the pointer to the end of the file. If the file specified by the $fie argument does not exist, then PHP attempts to create the file. Make sure your permissions are set correctly!

    • w+ As above, but the file may also be read.

    • a This mode is the same as the 'w' mode, with the exception that the internal pointer is placed at the end of the file. Existing contents will not be overwritten unless you rewind the internal pointer to the beginning of the file.

    • a+ As above, but the file may also be read.

Additionally, if the file is a binary file, you must include a "b" in the mode if your OS is Windows. This chapter only deals with ASCII text files, so you will not require the "b: mode in the following example. If you do need to open a binary file, an example is below:

 $file_handle = fopen($filename, "rb+")  

Note the differences between the "w" and "a" modes. Using "w" to open a file effectively deletes the contents of the file, while using the "a" mode retains the contents of the file and allows you to append additional data to the end of the file.

Once you have the file opened, you can then read from or write to the file.

Reading Files

There are multiple functions available to read from a file.

You can read data from the file using the fread() function:

 $file_contents = fread($file_pointer, $length_to_read);  

You must assign the value of fread() to a variable, which will contain the data that has been read. The fread() function takes two arguments:

  • $file_pointer is the file pointer to which you assigned the value from the fopen() function.

  • $length is the amount of bytes that you wish to read.

When using fread() to read a file, you can read in the entire file. To do this, you need to know the byte-size of the file. You can easily find the total byte-size of a file using the filesize() function:

 $bytes = filesize($filename);  

The following script demonstrates how to open a short text file and display its contents:

 $file = "/path/to/text.txt";  $filepointer = fopen($file, "r"); $contents = fread($filepointer, filesize($file)); echo "<pre>" . htmlentities($contents) . "</pre>"; 

There is a slight problem when reading files from the Internet: You cannot easily determine the size of the file, since it does not exist on the local server. An easy fix for this is to attempt to read many more bytes than you think exist in the file. Since fread() stops when it reaches the end of the file (EOF), you don't have to worry about PHP wasting processing on the extra bytes you specified for the $length argument in fread(). For example:

 $file = "http://www.example.com/text.txt";  $filepointer = fopen($file, "r"); $contents = fread($filepointer, 9999999); 

Closing Files

When you have finished reading from or writing to a file that has been opened using the fopen() function, you should always close it using the fclose() function:

 fclose($filepointer);  

Closing the file allows you to clean up any memory that was used to open it and also prevents possible file corruption that sometimes (rarely) occurs when a file is left open.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net