2012-08-03 114 views
1

我允许用户在我的网站上的ZIP压缩文档中上传投资组合。ZipArchive - 提取文件夹

的问题是,大多数的存档文件有以下文件夹结构:

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

无济于事。

+0

将压缩文件包含其中的子文件夹..? – 2012-08-03 11:47:13

回答

1

你可以做这样的事情:

  1. 提取Zip文件到一个临时位置。
  2. 扫描并将所有文件移动(复制)到portfolio文件夹。
  3. 删除临时文件夹及其所有内容(在步骤1中创建)。

代码:

//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; 
    } 
} 
-2

如何,如果改变你的zip文件到这一点?

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

这是不可能的,因为我不是控制zip文件的人。 – 2013-12-23 16:42:31