Extract files in a zip to root of a folder?

后端 未结 3 870
一个人的身影
一个人的身影 2020-12-21 11:27

I have a zip file uploaded to server for automated extract.

the zip file construction is like this:

/zip_file.zip/folder1/image1.jpg
/zip_file.zip/fo         


        
3条回答
  •  既然无缘
    2020-12-21 11:59

    Here: (i tried to manage everything)

    $zip = new ZipArchive();
    if( $zip->open($file_path) ){
        $files = array();
        for( $i = 0; $i < $zip->numFiles; $i++){
            $entry = $zip->statIndex($i);
            // is it an image?  
            if( $entry['size'] > 0 && preg_match('#\.(jpg)$#i', $entry['name'] ) ){
                $f_extract = $zip->getNameIndex($i);
                $files[] = $f_extract; /* you man want to keep this array (use it to show result or something else) */
    
                if ($zip->extractTo($dir_name, $f_extract) === TRUE) {
                    $solid_name = basename($f_extract);
                    if(strpos($f_extract, "/")) // make sure zipped file is in a directory
                    {
                        if($dir_name{strlen($dir_name)-1} == "/") $dir_name = substr($dir_name, 0, strlen($dir_name)-1); // to prevent error if $dir_name have slash in end of it
                        if(!file_exists($dir_name."/".$solid_name)) // you said you don't want to replace existed file
                            copy($dir_name."/".$f_extract, $dir_name."/".$solid_name); // taking file back to where you need [$dir_name]
                        unlink($dir_name."/".$f_extract); // [removing old file]
                        rmdir(str_replace($solid_name, "", $dir_name."/".$f_extract)); // [removing directory of it]
                    }
                } else {
                     echo("error on export
    \n"); } } } $zip->close(); }

提交回复
热议问题