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");
?>

2. Usage Delete Directory Function

<?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, 2012 Hoan 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 hoan@4rapiddev.com

7 years ago PHPclosedir, is_dir, opendir, readdir, rmdir, unlink100
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
18,697 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
14,446 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
13,326 views
JQuery Allow only numeric characters or only alphabet characters in textbox
10,871 views
C# Read Json From URL And Parse/Deserialize Json
7,461 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
  • Magento Fatal error: Unsupported operand types
  • Ionic bower the name contains uppercase letters
  • PHP Magento Get Manufacturer Name And ID From Magento Product ID
  • Know Simple Hacks To Systematize Media Library in WordPress
  • A handy guideline on adding custom menu item in WordPress admin
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (26)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (140)
EMAIL SUBSCRIPTION

Sign up for our newsletter to receive the latest news and event postings.

Recommended
  • CDN
  • Hosting
  • Premium Themes
  • VPS
2014 © 4 Rapid Development