Here is an example of the latter approach:
function delete_directory($dir)
{
if ($dh = opendir($dir))
{
302 CHAPTER 10 ?– WORKING WITH THE FI LE AND OPERATING SYSTEM
// Iterate through directory contents
while (($file = readdir ($dh)) != false)
{
if (($file == ".") || ($file == "..")) continue;
if (is_dir($dir . '/' . $file))
delete_directory($dir . '/' . $file);
else
unlink($dir . '/' . $file);
}
closedir($dh);
rmdir($dir);
}
}
$dir = "/usr/local/apache2/htdocs/book/chapter10/test/";
delete_directory($dir);
?>
Renaming a File
The rename() function renames a file, returning TRUE on success and FALSE otherwise.
Its prototype follows:
boolean rename(string oldname, string newname)
Because PHP scripts typically execute under the guise of the server daemon process
owner, rename() will fail unless that user has write permissions to that file.
Touching a File
The touch() function sets the file filename??™s last-modified and last-accessed times,
returning TRUE on success or FALSE on error. Its prototype follows:
int touch(string filename [, int time [, int atime]])
If time is not provided, the present time (as specified by the server) is used. If the
optional atime parameter is provided, the access time will be set to this value; otherwise,
like the modification time, it will be set to either time or the present server time.
Note that if filename does not exist, it will be created, assuming that the script??™s
owner possesses adequate permissions.
Pages:
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376