Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZipArchive-提取文件夹_Php_Ziparchive - Fatal编程技术网

Php ZipArchive-提取文件夹

Php ZipArchive-提取文件夹,php,ziparchive,Php,Ziparchive,我允许用户在我的网站上传ZIP档案中的公文包 问题在于,大多数归档文件具有以下文件夹结构: zipfile.zip - zipfile - file1.ext - file2.ext - file3.ext 有没有办法简单地把文件(而不是目录)放到我的网站上(所以他们的文件夹结构是这样的) 它当前会像这样上传它们: user_name - portfolio - zipfile - file1.ext - file2.ext

我允许用户在我的网站上传ZIP档案中的公文包

问题在于,大多数归档文件具有以下文件夹结构:

zipfile.zip
  - zipfile
    - 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();

没用。

您可以这样做:

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


    如果将您的zip文件更改为此,该如何操作

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

    压缩文件是否包含子文件夹?这是不可能的,因为我不是压缩文件的控制者。
     //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;
         }
    }
    
    zipfile.zip
      - file1.ext
      - file2.ext
      - file3.ext