2014-12-02 41 views
0

我有下面的代码。在编码后删除文件夹的代码故障

它首先创建一个名称为v3_12-02-2012-12873547839的动态文件夹。然后它创建一个名为“图像”的子文件夹,并在子文件夹中保存一些jpeg图像。然后,它创建一个CSV文件,并把它放在"v3_12-02-2012-12873547839"

然后在项目文件夹名称创建一个zip文件夹"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 .="\n"; 

    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 .="\n"; 
    } 
    $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); 

} 

现在的问题是,当我试图删除文件夹时,文件夹似乎不会删除,但我可以递归删除其所有内容。即使该文件夹最后变为空白,它也不会被删除。

错误我得到:中ZipArchive

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

的问题是,如果我发表意见然后rmdir()工作正常......这意味着,没有问题与scandir阿隆 – Saswat 2014-12-02 09:59:03

+0

然后'unset($ zip,$文件);'可能会关闭该文件夹的句柄。 – AmigoJack 2014-12-02 10:04:46

+0

圣牛!!!!!这有效的男人!请将此作为答案发布,所以我可以接受它。:) – Saswat 2014-12-02 10:09:40

回答

1

实例和/或RecursiveIteratorIterator仍然生活,可能仍然有他们的手放在你的目录,所以他们自由使用unset($zip, $files);