ZipArchive - 提取文件夹

时间:2012-08-03 11:19:34

标签: php ziparchive

我允许用户在我的网站上上传ZIP档案中的投资组合。

问题是大多数档案具有以下文件夹结构:

zipfile.zip
  - zipfile
    - file1.ext
    - file2.ext
    - file3.ext

有没有办法简单地将文件(而不是目录)放到我的网站上(所以他们的文件夹的文件夹结构是这样的)

user_name
  - portfolio
    - file1.ext
    - file2.ext
    - file3.ext

它目前上传的方式如下:

user_name
  - portfolio
    - zipfile
      - file1.ext
      - file2.ext
      - file3.ext

会产生各种各样的问题!

我试过这样做:

$zip = new ZipArchive();
$zip->open($_FILES['zip']['tmp_name']);
$folder = explode('.', $_FILES['zip']['name']);
end($folder);
unset($folder[key($folder)]);
$folder = (implode('.', $folder));
$zip->extractTo($root, array($folder));
$zip->close();

无济于事。

2 个答案:

答案 0 :(得分:1)

你可以这样做:

  1. 将Zip文件解压缩到临时位置。
  2. 扫描并将所有文件移动(复制)到portfolio文件夹。
  3. 删除临时文件夹及其所有内容(在步骤1中创建)。
  4. 代码:

     //Step 01
    $zip = new ZipArchive();
    $zip->open($_FILES['zip']['tmp_name']);
    $zip->extractTo('temp/user');
    $zip->close();
    
     //Define directories
    $userdir = "user/portfolio"; // Destination
    $dir = "temp/user";          //Source
    
     //Step 02
    // Get array of all files in the temp folder, recursivly
    $files = dirtoarray($dir);
    
    // Cycle through all source files to copy them in Destination
    foreach ($files as $file) {
        copy($dir.$file, $userdir.$file);
    }
    
     //Step 03
    //Empty the dir
    recursive_directory_delete($dir);
    
     // Functions Code follows..
    //to get all the recursive paths in a array
    function dirtoarray($dir, $recursive) {
        $array_items = array();
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (is_dir($dir. "/" . $file)) {
                        if($recursive) {
                            $array_items = array_merge($array_items, dirtoarray($dir. "/" . $file, $recursive));
                        }
                    } else {
                        $file = $dir . "/" . $file;
                        $array_items[] = preg_replace("/\/\//si", "/", $file);
                    }
                }
            }
            closedir($handle);
        }
        return $array_items;
    }
    
    // Empty the dir
    function recursive_directory_delete($dir)
    {
         // if the path has a slash at the end we remove it here
         if(substr($directory,-1) == '/')
         {
              $directory = substr($directory,0,-1);
         }
    
         // if the path is not valid or is not a directory ...
         if(!file_exists($directory) || !is_dir($directory))
         {
              // ... we return false and exit the function
              return FALSE;
    
         // ... if the path is not readable
         }elseif(!is_readable($directory))
         {
              // ... we return false and exit the function
              return FALSE;
    
         // ... else if the path is readable
         }else{
    
              // we open the directory
              $handle = opendir($directory);
    
              // and scan through the items inside
              while (FALSE !== ($item = readdir($handle)))
              {
                   // if the filepointer is not the current directory
                   // or the parent directory
                   if($item != '.' && $item != '..')
                   {
                        // we build the new path to delete
                        $path = $directory.'/'.$item;
    
                        // if the new path is a directory
                        if(is_dir($path))
                        {
                             // we call this function with the new path
                             recursive_directory_delete($path);
    
                        // if the new path is a file
                        }else{
                             // we remove the file
                             unlink($path);
                        }
                   }
              }
              // close the directory
              closedir($handle);
    
              // return success
              return TRUE;
         }
    }
    

答案 1 :(得分:-2)

如果将您的zip文件更改为此?

zipfile.zip
  - file1.ext
  - file2.ext
  - file3.ext