05May2011
Author
Dave
Category
PHP
Tags
, , , ,
Delete folders from server using PHP Thumbnail

Delete folders from server using PHP

A tutorial to create a function to delete folder and all sub folders from the server.

A tutorial to create a function to delete folder and all sub folders from the server.

start by stating it’s a function by issuing the word function followed by a name in this case delete_directory with a parameter this is a variable that contains the name of the directory you want to delete

<?php
function delete_directory($dirname) {
?>

Then an if statement is used to see if the directory is a real directory and if it is then open the directory

<?php
if (is_dir($dirname)){
$dir_handle = opendir($dirname);
}
?>

then anther if statement is done to see if the directory is valid if it’s not then return false is set and the directory won’t be deleted.

<?php
if (!$dir_handle){
return false;
}
?>

Then a while loop is performed reading the directory contents and adding them at a viable called file to check the files have valid names then delete them from the server using unlink, if the files are invalid then the function is called again with the file name in question.

<?php
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
?>

Then the directory is closed using closedir then it is removed using readir then returns a success status by using true.

to run the function you need to call it you do so by typing the function name with a parameter inside parenthesis.

<?php
delete_directory('foldername');
?>
<?php
closedir($dir_handle);
rmdir($dirname);
return true;
}
?>

Here’s the full script:

<?php
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('foldername');?>

That’s it run the script to delete any folder from your server using the magic of php!

Author
Dave

About the Author

has written 72 articles on Dave is my name.

My name is David Carr I'm a PHP web developer based in Hull it is my passion to design and develop clean, accessible and usable websites using PHP and other web technologies, I'm an avid Twitter user why not follow me? https://twitter.com/daveismynamecom

Visit this author's website   ·   View more posts by

Sharing is caring.
  • Subscribe to our feed
  • Share this post on Delicious
  • StumbleUpon this post
  • Share this post on Digg
  • Tweet about this post
  • Share this post on Mixx
  • Share this post on Technorati
  • Share this post on Facebook
  • Share this post on NewsVine
  • Share this post on Reddit
  • Share this post on Google
  • Share this post on LinkedIn

Discussion

No responses to "Delete folders from server using PHP"

There are no comments yet, add one below.

Leave a Comment