Search code examples
phpdestroyunlink

Deleting contents of all folders


I have a table with the names of all the folders I wish to delete the contents off. Now I have a script which will delete the entire contents of a folder that I set. Now I though I could put that code in a while loop and it would delete the contents of all the folders. However, I get an error. Here is the code, error is at the bottom, what's going wrong and how do I fix this?

$query = "SELECT * FROM gemeentes"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){


$gemeente1 = str_replace(" ","",$row['gemeente']);
$gemeente2 = strtolower($gemeente1);
$gemeente3 = str_replace("(","-",$gemeente2);
$gemeente4 = str_replace(")","",$gemeente3);
$gemeente5 = str_replace(",","",$gemeente4);






if(isset($_POST['GO'])) {


$directory = "../subdomains/".$gemeente5."/httpdocs/";


echo $directory;




define('PATH', $directory);


function destroy($dir) {

    $mydir = opendir($dir);

    while(false !== ($file = readdir($mydir))) {

        if($file != "." && $file != "..") {

            chmod($dir.$file, 0777);

            if(is_dir($dir.$file)) {

                chdir('.');

                destroy($dir.$file.'/');

                rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");

            }

            else

                unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");

        }

    }

    closedir($mydir);

}

destroy(PATH);

echo 'all done.'; 





}







}

The first delete comes back fine, the second won't do the trick anymore:

../subdomains/aaenhunze/httpdocs/all done.../subdomains/aalburg/httpdocs/
Fatal error: Cannot redeclare destroy() (previously declared in /vhosts/url.nl/httpdocs/deletecontent.php:50) in /vhosts/url.nl/httpdocs/deletecontent.php on line 50

Solution

  • Why are you calling your function as destroy(PATH); with a "define"d constant instead of just the actual underlying variable as: destroy($directory);? Once you take the function out of the loop as Bulk suggested, this should work I'd think...