Managing Files

Let's examine how PHP allows you to work with files stored on a web server's hard disk.

File Permissions

Before you can perform any filesystem access from PHP, you have to consider the permission settings for the files you want to work with. This section deals primarily with the file permissions system on Unix/Linux systems, but the same considerations apply to all platforms.

Usually your web server will be running under either the apache or nobody username, yet your web documents and PHP scripts will be owned by your actual system user. Unless the web server user has permissions to access your files, any access attempts will fail.

To grant to all users read-only access to a file, you use the chmod command, which sets the read flag (r) on the file for all users other than the current one (o):

 $ chmod o+r filename 

You can also set global read/write permissions on a file by using chmod as follows:

 $ chmod o+rw filename 

Refer to man chmod for full details and more examples.

If a file has global permissions, the web server will be able to access it. However, any other user on your system will also have full access to these files. In some situations you might prefer to change the ownership on a file to the apache user rather than grant global write access. The superuser does this by using the chown command:

 # chown apache filename 

The PHP function chmod can also be used to alter file permissions. It takes two arguments: a filename and a mode. The mode argument can be either the string type, such as o+rw, or the numeric type, such as 0644, as long as the number is prefixed with a zero.

Using chmod If you are familiar with the system command chmod, you may be used to giving a three-digit number as the mode argument. When run from the shell, chmod assumes that an octal value is given, whether or not it is prefixed with a zero; for example, chmod 644 is the same as chmod 0644. In PHP, however, the leading zero is always required in the argument to chmod.

Getting Information About a File

PHP provides a wide range of functions for getting information about a file on your system. The simplest, and one you may use often, is file_exists, which simply tells you whether there is a file with the given name argument. The file_exists function returns TRue if any item on the filesystem has the given name, even if it is a directory or a special file type. The argument may contain a path, as shown here:

 if (file_exists("/home/chris/myfile")) {   echo "The file exists"; } else {   echo "The file does not exist"; } 

A number of other functions allow you to test certain attributes of a file. These are shown in Table 17.1.

Table 17.1. Functions for Testing Attributes of a File




Checks whether the file has the executable attribute.


Checks whether the file is readable.


Checks whether the file is writeable.


Checks for a symbolic link.


Checks for a real file, not a link.

Yet other functions return information about the file itself. These are shown in Table 17.2.

Table 17.2. Functions That Find Information About a File




Checks the time of the last file access, as a Unix timestamp.


Checks the time of file inode creation.


Checks the time of the last modification to the file.


Checks the user ID of the file owner.


Checks the group ID of the file.


Checks the inode number of the file.


Checks the file's permission settings as an octal value (for example, 0644).


Checks the size of the file in bytes.


Checks the type of the file (fifo, char, dir, block, link, or file).

Moving and Copying Files

Assuming that you have permission to do so, you can perform a file copy, move, or delete operation from PHP. The functions for these actions are copy, rename, and unlink, respectively.

The copy and rename functions take two argumentsthe source and destination filenameswhereas unlink takes a single filename.

File Paths You should be particularly careful when performing file operations from PHP, particularly when deleting. You need to always make sure you know what the current working directory is, or give a full path to the target file.

Working with Filenames

The functions basename and dirname provide an easy way to dissect a string into a filename and path, respectively. You might use these functions to find out the base filename when a full path is given or to find the pathname if you want a file created with other files in the same place as a known filename.

The basename function returns everything from the last slash character in the string to the end, whereas dirname returns the portion of the string before this slash.

The realpath function takes a pathname argument and returns its absolute pathname. Any symbolic links in the path are resolved to their actual location on disk, and any references to the current or parent directory using . or .. are removed.

If you want to write to a temporary file, you can use the tempnam function to generate a unique temporary filename. It takes two argumentsa directory name and a filename prefix. The prefix argument is required but can be an empty string. The following statement generates a temporary filename in /tmp with no prefix:

 $filename = tempnam("/tmp", ""); 

    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: