Приглашаем посетить
Культурология (cult-lib.ru)

Opening and Closing Files

Previous
Table of Contents
Next

Opening and Closing Files

($fp = @fopen('file.txt', 'at')
fclose($fp);


For most file system functions, the files have to be opened first. The function fopen() does exactly this and returns a so-called file handle, a pointer to the file. This file handle can then be used in subsequent functions to, for instance, read information from a file or write to it.

fopen() expects at least two parameters:

  • The name of the file

  • The file mode to use when accessing the file

Opening (and Closing) Files (fopen.php)
<?php
  if ($fp = @fopen('file.txt', 'at')) {
    echo 'File opened.';
    fclose($fp);
    echo '<br />File closed.';
  } else {
    echo 'Error opening file.';
  }
?>

Of great interest is the file mode parameter. This is a string that consists of one or more characters. The first character is one of a, r, w, or x; after that, one or more special modifiers can be used. Table 6.1 shows all modes.

Table 6.1. File Modes for PHP's File Functions

Mode

Description

a

Open file to append (write) data; create it if it doesn't exist

a+

As mode a, but additionally with read access to the file

r

Open file to read data

r+

As mode r, but in addition with write access to the file

w

Open file to write data, erasing its contents; create it if it doesn't exist

w+

As mode w, but in addition with read access to the file

x

Create file to write data; send E_WARNING if it already exists

x+

As mode x, but in addition with read access to the file


So the modifier + always adds the missing read or write access to a file mode. There are other modifiers, as well. As of PHP 4.3.2, PHP does a really good job determining whether a file is a text file or a binary file and translates funny characters appropriately. If you append b to a file mode, you force PHP to open a file as a binary file; for instance, the file mode rb opens a file for reading in binary mode.

Another special modifier exists that might come in handy for Windows users: t. If this is appended to the file mode, all \n line breaks are converted into \r\n as Windows applications might expect.

fopen() returns a file handle or false if opening the file did not work. You should, however, suppress any potential error messages with the @ character, or, in PHP 5, use a TRy-catch block.

As soon as a file is not used anymore, it should be closed. PHP automatically closes all open files upon termination of the script; however, to use the system resources as efficiently as possible, files should be closed as early as possible to free up memory and speed up the system. For this, use fclose() and provide the file handle as a parameter.

The code at the beginning of this phrase uses both fopen() and fclose().

After running the script, a file called file.txt is created.

NOTE

For this (and other examples in this chapter) to work, you have to make sure that the PHP script has the appropriate rights for the file to access. Usually, Apache runs either under the special user id 0 (UNIX/Linux) or under a regular user account (Windows). Use chmod to set the access privileges accordingly.

Microsoft IIS (Internet Information Services) runs PHP scripts under a special guest account called IUSR_<name of machine>. You can use the graphical user interface shown in Figure 6.1 to tune the access settings for files or directories.

Figure 6.1. Allowing the IIS user to access a file or directory.

Opening and Closing Files



TIP

To avoid any mistakes when trying to access nonexisting files, you can use the function file_exists(), which returns regardless of whether a given filename exists.

if (file_exists('file.txt'), 'r') {
  // ...
}



Previous
Table of Contents
Next