HomePHP Page 6 - File And Directory Manipulation In PHP (part 2)
Move It - PHP
Now that you know the basics of reading and writing files, this second segment of our tutorial on the PHP filesystem API takes you into deeper waters, showing you how to copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files.
In addition to offering you path information, PHP comes with a whole bunch of functions designed to simplify the task of moving, copying, renaming and deleting files on the filesystem. The first of these is the copy() function, which accepts two arguments, a source file and a destination, and copies the former to the latter. Here's an example which demonstrates:
<?php
// check to see if file exists
// if so, back it up
if (file_exists("matrix.txt"))
{
copy ("matrix.txt", "matrix.txt.orig") or die ("Could
not copy file"); }
?>
Note that if the destination file already exists, copy() will usually overwrite it. A failure to copy the file will cause copy() to return false; success returns true.
A corollary to the copy() function is the rename() function, which can be used to both rename and move files. Like copy(), it too accepts two arguments, a source file and a destination file. Consider the following example, which renames a file,
<?php
// check to see if file exists
// if so, rename it
if (file_exists("matrix.txt"))
{
rename ("matrix.txt", "neoworld.txt") or die ("Could
not rename file"); }
?>
and this one, which simultaneously renames and moves a file.
<?php
// check to see if file exists
// if so, move and rename it
if (file_exists("/home/john/newsletters"))
{
rename ("/home/john/newsletters", "/home/john/mail/lists")
or die ("Could not move file"); }
?>
It's possible to rename directories in the same manner as files - as illustrated in this next snippet:
<?php
// check to see if directory exists
// if so, rename it
if (is_dir("Mail"))
{
rename ("Mail", "mail-jun-03") or die ("Could not rename
directory"); }
?>
The rename() function comes in handy when you need to update files which are constantly being used by multiple processes. Instead of directly updating the target file with new data, rename() allows you to copy the original file contents into a new file, make your changes and then, once you're happy with the result, simply rename() the new file to the old one.
The following example demonstrates:
<?php
// read file
$contents = file("/etc/inittab") or die ("Could not read file");
// add a line to it
$contents[] = "x:5:respawn:/etc/X11/prefdm -nodaemon";
// write contents to temporary file
$tmpfile = tempnam("/tmp", "inittab.tmp.");
// open file
$fh = fopen ($tmpfile, "w") or die("Could not open file");
foreach ($contents as $line)
{
fwrite ($fh, $line) or die ("Could not write file");
}
// close file
fclose ($fh);
// move temporary file over original
rename($tmpfile, "/etc/inittab") or die ("Could not replace file");
?>
Note my use of the tempnam() function above - this function generates a unique file name, given a directory and a filename prefix, and can help to avoid filename collisions between different processes.
When it comes time to delete files, PHP offers the unlink() function, which can be used to erase a file from the filesystem. Consider the following example, which demonstrates by deleting a specific file:
<?php
// check to see if file exists
// if so, erase it
if (file_exists("error.log"))
{
unlink ("error.log") or die ("Could not delete file");
}
?>
You can also use the unlink() function to iterate over a directory and remove all the files within it - this is demonstrated in an example coming up shortly.
Note that the unlink() function (and indeed, all other file manipulation functions) will fail if the user ID under which the Web server is running does not have adequate permissions to delete or otherwise modify the named file(s).