Home arrow PHP arrow Page 10 - File And Directory Manipulation In PHP (part 2)

Purging The Dead - 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.

  1. File And Directory Manipulation In PHP (part 2)
  2. Stripping It To The Bone
  3. Fertile Fields
  4. Configuring The System
  5. The Right Path
  6. Move It
  7. Beam Me Up
  8. Diving Into Directories
  9. A Pattern Emerges
  10. Purging The Dead
  11. Size Does Matter
  12. In Process
  13. Disk Full
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 65
August 21, 2003

print this article



Not only does PHP let you read directory contents, it also allows you to create and delete directories with the mkdir() and rmdir() functions respectively. Consider the following example, which creates a directory,


// create directory
mkdir("/tmp/stuff") or die ("Could not make directory");


and its mirror image, which removes the newly-created directory.


// erase directory
rmdir("/tmp/stuff") or die ("Could not erase directory");


As with the other filesystem manipulation functions, these functions too will fail if the user the Web server is running as lacks sufficient privileges to create and delete directories on the disk. Directories created with mkdir() will be owned by the process running the Web server.

It's interesting also to note that the rmdir() function operates only if the directory to be deleted is completely empty. Try running it on a directory containing existing files, as below,


// create directory
mkdir("/tmp/stuff") or die ("Could not make directory");

// create sub-directory
mkdir("/tmp/stuff/personal") or die ("Could not make directory");

// remove parent directory
rmdir("/tmp/stuff") or die ("Could not remove directory");


and you'll be rewarded with the following error:

Warning: mkdir(): File exists in /home/web/rmdir.php on line 4 Could not remove directory

Since it's unlikely that you'll find empty directories just waiting for your rmdir() in the real world, you'll normally need to empty the directory manually prior to calling rmdir() on it. The following example demonstrates, by combining the unlink() function discussed previously with some recursive logic to create a function designed specifically to erase the contents of a directory (and its sub-directories) in one fell swoop:


// recurses into a directory and empties it
// call it like this: purge("/dir/")
// remember the trailing slash!
function purge($dir)
$handle = opendir($dir);
while (false !== ($file = readdir($handle)))
if ($file != "." && $file != "..")
if (is_dir($dir.$file))
purge ($dir.$file."/");


>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: