删除所有文件夹的内容

时间:2012-08-02 11:24:45

标签: php destroy unlink

我有一个表格,其中包含我要删除内容的所有文件夹的名称。现在我有一个脚本,它将删除我设置的文件夹的全部内容。现在我虽然可以将该代码放在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

3 个答案:

答案 0 :(得分:1)

为什么使用“define”d常量调用函数destroy(PATH);而不仅仅是实际的基础变量:destroy($directory);?一旦你按照Bulk的建议把这个函数带出循环,这应该可以工作我想......

答案 1 :(得分:0)

您正在最外层while循环中定义destroy函数,因此第二次运行循环时,函数已经定义。将函数定义移动到while循环外部以修复此问题。

答案 2 :(得分: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);

}