Jump to content

[SOLVED] How to empty a directory


ballouta

Recommended Posts

<?php

function empty_dir($dir); //$dir should NOT be passed with an ending "/".
{
$result = true;

$h = opendir($dir);
if($h === false)
	return false;
while($file = readdir($h))
{
	if($file == "." || $file == "..")
		continue;
	if(is_dir($file))
	{
		if(!empty_dir($dir."/".$file))
			$result = false;
		continue;
	}
	unlink($file); //Regular file..
}
closedir($dir);

return $result;
}

$dir = "/home/gscl/public_html/stk/temp";
if(!empty_dir($dir))
echo "An error occured. Some of the files weren't deleted.";
else
echo "Dir empty.";

?>

 

 

Haven't tested it, use at your own risk.

 

Orio.

Hi

i changed this: $dir = "/home/gscl/public_html/stk/on";

on is an existing directory and contains 6 files

 

I run the code but didn't delete anything:

I got this:

Warning: unlink(172129.ask) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

 

Warning: unlink(357934004753637.ask) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

 

Warning: unlink(172129nss.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

 

Warning: unlink(172129.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

 

Warning: unlink(357934004753637.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

 

Warning: unlink(357934004753637nss.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212

I found this one it is working properly

thanks

 

<?php
function deleteDir($dir) {
   // open the directory
   $dhandle = opendir($dir);

   if ($dhandle) {
      // loop through it
      while (false !== ($fname = readdir($dhandle))) {
         // if the element is a directory, and 
         // does not start with a '.' or '..'
         // we call deleteDir function recursively 
         // passing this element as a parameter
         if (is_dir( "{$dir}/{$fname}" )) {
            if (($fname != '.') && ($fname != '..')) {
               echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />";
               deleteDir("$dir/$fname");
            }
         // the element is a file, so we delete it
         } else {
            echo "Deleting File: {$dir}/{$fname} <br />";
            unlink("{$dir}/{$fname}");
         }
      }
      closedir($dhandle);
    }
   // now directory is empty, so we can use
   // the rmdir() function to delete it
   echo "<u>Deleting Directory</u>: {$dir} <br />";
   rmdir($dir);
}

// call deleteDir function and pass to it 
// as a parameter a directory name
deleteDir("temporary");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.