Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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中提取ZIP文件的子文件夹_Php_Zip_Unzip - Fatal编程技术网

在PHP中提取ZIP文件的子文件夹

在PHP中提取ZIP文件的子文件夹,php,zip,unzip,Php,Zip,Unzip,我正在使用一个php脚本来解压缩文件。但是这个脚本只解压缩一级目录,而不提取该文件的子目录 剧本: $zip = new ZipArchive; if ($zip->open('test.zip') === TRUE) { $zip->extractTo('/my/destination/dir/'); $zip->close(); echo 'ok'; } else { echo 'failed'; } 例如:如果test.zip包含两个文件

我正在使用一个php脚本来解压缩文件。但是这个脚本只解压缩一级目录,而不提取该文件的子目录 剧本:

$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
例如:如果test.zip包含两个文件夹:folder1\file.png、folder2\folder3\file3.png

解压缩此ZIP文件后,我只看到folder1**和folder2***,但没有子目录folder3


如何改进它?

我认为这本PHP手册将对您有所帮助


尝试以下简单方法:

system('unzip my_zip_file.zip');

此脚本递归提取zip文件中的所有文件。这将把它们解压缩到当前目录中

    <?php
    set_time_limit(0);

    echo "HI<br><br>";
    //----------------
    //UNZIP a zip file
    //----------------

    $zipfilename = "site.zip";

    //----------------

    function unzip($file){

        $zip=zip_open(realpath(".")."/".$file);
        if(!$zip) {return("Unable to proccess file '{$file}'");}

        $e='';

        while($zip_entry=zip_read($zip)) {
           $zdir=dirname(zip_entry_name($zip_entry));
           $zname=zip_entry_name($zip_entry);

           if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;}
           if(!is_dir($zdir)) mkdirr($zdir,0777);

           #print "{$zdir} | {$zname} \n";

           $zip_fs=zip_entry_filesize($zip_entry);
           if(empty($zip_fs)) continue;

           $zz=zip_entry_read($zip_entry,$zip_fs);

           $z=fopen($zname,"w");
           fwrite($z,$zz);
           fclose($z);
           zip_entry_close($zip_entry);

        }
        zip_close($zip);

        return($e);
    }

    function mkdirr($pn,$mode=null) {

      if(is_dir($pn)||empty($pn)) return true;
      $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn);

      if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;}

      $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
      if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
      return false;
    }

    unzip($zipfilename);

    ?>
这有帮助吗:?
    <?php
    set_time_limit(0);

    echo "HI<br><br>";
    //----------------
    //UNZIP a zip file
    //----------------

    $zipfilename = "site.zip";

    //----------------

    function unzip($file){

        $zip=zip_open(realpath(".")."/".$file);
        if(!$zip) {return("Unable to proccess file '{$file}'");}

        $e='';

        while($zip_entry=zip_read($zip)) {
           $zdir=dirname(zip_entry_name($zip_entry));
           $zname=zip_entry_name($zip_entry);

           if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;}
           if(!is_dir($zdir)) mkdirr($zdir,0777);

           #print "{$zdir} | {$zname} \n";

           $zip_fs=zip_entry_filesize($zip_entry);
           if(empty($zip_fs)) continue;

           $zz=zip_entry_read($zip_entry,$zip_fs);

           $z=fopen($zname,"w");
           fwrite($z,$zz);
           fclose($z);
           zip_entry_close($zip_entry);

        }
        zip_close($zip);

        return($e);
    }

    function mkdirr($pn,$mode=null) {

      if(is_dir($pn)||empty($pn)) return true;
      $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn);

      if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;}

      $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
      if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
      return false;
    }

    unzip($zipfilename);

    ?>