Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Completely Delete A Directory Include Sub Directories And Files

PHP Completely Delete A Directory Include Sub Directories And Files

Below is just a PHP function that delete a particular directory/folder, all its sub directories/folders and files.

1. PHP Completely Removes A Specified Directory

<?php
	ini_set('display_errors', 1);
	ini_set('log_errors', 1);
 
	function delete_directory( $dirname ) {
		if ( is_dir( $dirname ) ) {
			$dir_handle = opendir( $dirname );
		}
		if ( !$dir_handle ) {
			return false;
		}
		while ( $file = readdir( $dir_handle ) ) {
			if ( $file != '.' && $file != '..' ) {
				if ( !is_dir( $dirname.'/'.$file ) ) {
					unlink( $dirname.'/'.$file );
				} else {
					delete_directory( $dirname.'/'.$file );
				}
			}
		}
		closedir( $dir_handle );
		rmdir( $dirname );
		return true;
	}
 
	delete_directory("D:/dev/web/htdocs/4rapiddev/demo/_tmp");
	delete_directory("D:/_tmp");
?>

<?php ini_set('display_errors', 1); ini_set('log_errors', 1); function delete_directory( $dirname ) { if ( is_dir( $dirname ) ) { $dir_handle = opendir( $dirname ); } if ( !$dir_handle ) { return false; } while ( $file = readdir( $dir_handle ) ) { if ( $file != '.' && $file != '..' ) { if ( !is_dir( $dirname.'/'.$file ) ) { unlink( $dirname.'/'.$file ); } else { delete_directory( $dirname.'/'.$file ); } } } closedir( $dir_handle ); rmdir( $dirname ); return true; } delete_directory("D:/dev/web/htdocs/4rapiddev/demo/_tmp"); delete_directory("D:/_tmp"); ?>

2. Usage Delete Directory Function

<?php
	delete_directory("D:/dev/web/htdocs/4rapiddev/demo/_tmp");
	delete_directory("D:/_tmp");
?>

<?php delete_directory("D:/dev/web/htdocs/4rapiddev/demo/_tmp"); delete_directory("D:/_tmp"); ?>

Note:

  • 1. The directory has to have Write Permission
  • 2. The function above deletes into sub directories recursively.
  • 3. The deleted directory must be inside the current web space.
Mar 1, 2012Hoan Huynh
innerText May Not Work On Mozilla FirefoxConnect Remotely To MySQL or SQL Server Databases for Godaddy Hosting Account
You Might Also Like:
  • PHP Delete File Function
  • PHP Copy Entire Directory To Another Directory
  • PHP check if folder exists then create folder
  • Linux remove entire directory
  • Free Software To Monitor and Log Directories Files Changed On Windows 2000 XP 2003 Vista 7 Server 2008
  • Fix SoftException in Application.cpp Directory/File is writeable by group
  • Linux Change Owner Directory Recursively
  • Linux Zip Entire Directory Recursively
  • PHP Read File Using Fread() Simple Example
  • Protected Web Directories With .htaccess
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHPclosedir, is_dir, opendir, readdir, rmdir, unlink128
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,196 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,069 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,847 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,323 views
C# Read Json From URL And Parse/Deserialize Json
9,815 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development