0

我有一张表,其中包含我希望删除内容的所有文件夹的名称。现在我有一个脚本,它将删除我设置的文件夹的全部内容。现在我虽然可以将该代码放在一个while循环中,它会删除所有文件夹的内容。但是,我得到一个错误。这是代码,错误在底部,出了什么问题,我该如何解决?

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





}







}

第一个删除恢复正常,第二个不再起作用:

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

3 回答 3

1

destroy(PATH);你为什么用“定义”的常量来调用你的函数,而不仅仅是实际的底层变量: destroy($directory);?一旦按照 Bulk 的建议将函数从循环中取出,我认为这应该可以工作......

于 2012-08-02T11:44:04.937 回答
0

destroy在最外面的 while 循环内定义函数,因此第二次运行循环时,该函数已经定义。将函数定义移到 while 循环之外以解决此问题。

于 2012-08-02T11:28:41.237 回答
0

谢谢大家的帮助。我看了你所有的答案,OzgurH 的建议起到了作用。工作代码:

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

}
于 2012-08-02T11:57:23.293 回答