19.12. Removing a Directory and Its Contents19.12.1. ProblemYou want to remove a directory and all of its contents, including subdirectories and their contents. 19.12.2. SolutionOn Unix, use rm: $directory = escapeshellarg($directory); exec("rm -rf $directory"); On Windows, use rmdir: $directory = escapeshellarg($directory); exec("rmdir /s /q $directory"); 19.12.3. DiscussionRemoving files, obviously, can be dangerous. Be sure to escape $directory with escapeshellarg( ) so that you don't delete unintended files. Because PHP's built-in directory removal function, rmdir( ) , works only on empty directories, and unlink( ) can't accept shell wildcards, calling a system program is much easier than recursively looping through all files in a directory, removing them, and then removing each directory. If an external utility isn't available, however, you can modify the pc_process_dir( ) function from Recipe 19.10 to remove each subdirectory. 19.12.4. See AlsoDocumentation on rmdir( ) at http://www.php.net/rmdir; your system's rm or rmdir documentation, such as the Unix rm(1) manpage or the Windows rmdir /? help text. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|