dsv73806 2014-12-02 09:36
浏览 28
已采纳

在制作zip之后用于删除文件夹的代码中的毛刺

I have the code below.

It first create a dynamic folder with name like v3_12-02-2012-12873547839. Then it creates a subfolder called "image" and saves some jpeg images in the subfolder. Then it create a csv file and put it in "v3_12-02-2012-12873547839"

Then it creates a zip folder in the project folder with name "v3_12-02-2012-12873547839.zip"

function create_csv($version,$ctg,$cnt,$nt,$api)
{

    $folder = $version."-".date('d-m-Y')."-".time();

    if(!file_exists('./'.$folder))
    {
        mkdir('./'.$folder);
        mkdir('./'.$folder.'/image/');
    }   

    $cnt_table = "aw_countries_".$version;
    $ctg_table = "aw_categories_".$version;
    $off_table = "aw_offers_".$version;


    $sizeof_ctg = count($ctg);
    $cond_ctg = " ( ";
    for($c = 0; $c < $sizeof_ctg ; $c++)
    {
        $cond_ctg = $cond_ctg." $ctg_table.category = '".$ctg[$c]."' ";
        if($c < intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." OR ";
        else if($c == intval($sizeof_ctg-1))
            $cond_ctg = $cond_ctg." ) ";
    }

    $sizeof_cnt = count($cnt);
    $cond_cnt = " ( ";
    for($cn = 0; $cn < $sizeof_cnt ; $cn++)
    {
        $cond_cnt = $cond_cnt." $cnt_table.country = '".$cnt[$cn]."' ";
        if($cn < intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." OR ";
        else if($cn == intval($sizeof_cnt-1))
            $cond_cnt = $cond_cnt." ) ";
    }

    $sizeof_nt = count($nt);
    $cond_nt = " ( ";
    for($n = 0; $n < $sizeof_nt ; $n++)
    {
        $cond_nt = $cond_nt." $off_table.network_id = '".$nt[$n]."' ";
        if($n < intval($sizeof_nt-1))
            $cond_nt = $cond_nt." OR ";
        else if($n == intval($sizeof_nt-1))
            $cond_nt = $cond_nt." ) ";
    }

    $sizeof_api = count($api);
    $cond_api = " ( ";
    for($a = 0; $a < $sizeof_api ; $a++)
    {
        $cond_api = $cond_api." $off_table.api_key = '".$api[$a]."' ";
        if($a < intval($sizeof_api-1))
            $cond_api = $cond_api." OR ";
        else if($a == intval($sizeof_api-1))
            $cond_api = $cond_api." ) ";
    }

    $output         = "";

    $sql = "SELECT DISTINCT $off_table.id,$off_table.name
            FROM $off_table,$cnt_table,$ctg_table
            WHERE  $off_table.id = $cnt_table.id
            AND $off_table.id = $ctg_table.id
            AND ".$cond_api."
            AND ".$cond_nt."
            AND ".$cond_cnt."
            AND ".$cond_ctg;

    $result = mysql_query($sql);

    $columns_total  = mysql_num_fields($result);

    for ($i = 0; $i < $columns_total; $i++) 
    {
        $heading    =   mysql_field_name($result, $i);
        $output     .= '"'.$heading.'",';
    }
    $output .= '"icon"';
    $output .="
";

    while ($row = mysql_fetch_array($result)) 
    {
        for ($i = 0; $i < $columns_total; $i++) 
        {
            $output .='"'.$row["$i"].'",';
        }
        $sql_icon = "SELECT $off_table.icon FROM $off_table WHERE id = '".$row['id']."'";
        $result_icon = mysql_query($sql_icon);
        while($row_icon = mysql_fetch_array($result_icon)) 
        {
            $image = $row_icon["icon"];
            $id = $row["id"];
            $icon = "./$folder/image/{$id}.jpg";
            $icon_link = "$folder/image/{$id}.jpg";
            file_put_contents($icon, $image);
        }
        $output .= '"'.$icon_link.'"';
        $output .="
";
    }
    $filename =  "myFile.csv";
    $fd = fopen ( "./$folder/$filename", "w");
    fputs($fd, $output);
    fclose($fd);

    $source = $folder;
    $destination = $folder.'.zip';


    $flag = '';

    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', $source);

    if($flag)
    {
        $flag = basename($source) . '/';
    }

    if (is_dir($source) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file)
        {
            if (strpos($flag.$file,$source) !== false) { // this will add only the folder we want to add in zip

                if (is_dir($file) === true)
                {

                }
                else if (is_file($file) === true)
                {
                    $zip->addFromString(str_replace($source . '/', '', $flag.$file), file_get_contents($file));
                }
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString($flag.basename($source), file_get_contents($source));
    }
    $zip->close();

    if (is_dir($folder)) 
    { 
        $objects = scandir($folder); 
        foreach ($objects as $object) 
        { 
           if ($object != "." && $object != "..") 
           { 
                if (filetype($folder."/".$object) == "dir") 
                {
                    $object_inner = scandir($folder."/".$object);
                    foreach ($object_inner as $object_inner) 
                    { 
                       if ($object_inner != "." && $object_inner != "..") 
                       { 
                            unlink($folder."/".$object."/".$object_inner); 
                       } 
                    } 
                    rmdir($folder."/".$object); 
                }   
                else 
                    unlink($folder."/".$object); 
           } 
        } 
        reset($objects); 
    } 
    rmdir("./".$folder);

}

Now the problem is, when I am trying to delete the folder, the folder somehow doesn't seem to delete, though I can recursively delete all its contents. Even though the folder becomes empty at the end, it doesn't get deleted.

Error I am getting:

Warning: rmdir(./v3-02-12-2014-1417512727): Permission denied in C:\xampp\htdocs\projecthas2offer\appwall_dev\frontend\ajax.php on line 265
  • 写回答

1条回答 默认 最新

  • dqm4977 2014-12-02 10:14
    关注

    Instances of ZipArchive and/or RecursiveIteratorIterator still live and might still have their hands on your directory, so free them using unset( $zip, $files );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法