|
Let's examine how PHP allows you to work with files stored on a web server's hard disk. File PermissionsBefore 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.
Getting Information About a FilePHP 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.
Yet other functions return information about the file itself. These are shown in Table 17.2.
Moving and Copying FilesAssuming 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.
Working with FilenamesThe 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", ""); |
|