1

我允许用户在我的网站上上传 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();

无济于事。

4

2 回答 2

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;
     }
}
于 2012-08-03T13:24:28.137 回答
-2

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

zipfile.zip
  - file1.ext
  - file2.ext
  - file3.ext
于 2013-12-21T04:45:22.363 回答