0

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
user1555076
  • 327
  • 2
  • 5
  • 16
  • why not `exec('rm -rf '.PATH,$r,$s);` or `system('rm -rf '.PATH);` and let the OS do the job of removing the files and folders, it's the better tool for the job IMO. Other then that: try `require_once`, it seems like you're either including this script several times (redeclaring the function each time, hence the error) or check if you happen to have more then 1 `destroy` function somewhere – Elias Van Ootegem Aug 02 '12 at 11:33

3 Answers3

1

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...

OzgurH
  • 443
  • 2
  • 13
0

You are defining the destroy function inside the outermost while loop, so the second time it comes to run the loop the function is already defined. Move the function definition to outside of the while loop to fix this.

Dan Smith
  • 5,685
  • 32
  • 33
  • I understand the problem is that one function can't be declared more than once. The solutions would indeed be, placing it outside the wile loop. However: how would I do this without breaking the entire functionality of the code? – user1555076 Aug 02 '12 at 11:32
  • I dont think it would - there is nothing in the function that I can see that needs it to be in that location. Have you tried doing it? Just copy it all and move it to the end of the file, see what happens. – Dan Smith Aug 02 '12 at 11:38
0

Thank you all for the help. I took a look at all your answers and what OzgurH suggested did the trick. The working code:

$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;





destroy($directory);

echo 'all done.'; 





}







}


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

}
user1555076
  • 327
  • 2
  • 5
  • 16