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,
and its mirror image, which removes the newly-created 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,
and you'll be rewarded with the following error:
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:
blog comments powered by Disqus |