php ZipArchive multi-file download package


public function downLoad($dataUrl,$saveName) { $datalist = [ ROOT_PATH.'/public/introduce/110.docx', ROOT_PATH.'/public/upfile/110.zip' ]; // print_r($datalist);die; $filename = ROOT_PATH.'\public\/'.$saveName.'.zip'; if(file_exists($filename)){ unlink($filename); } $zip = new\ ZipArchive (); IF ( $ ZIP -> Open ( $ filename !, \ :: ZipArchive the CREATE) == to true ) { Exit ( 'Unable to open file, or file creation failed' ); } foreach ( $ dataUrl AS $ index => $ Item ) { IF (DIRECTORY_SEPARATOR == '\\' ) { $ Item = str_replace ( '/', DIRECTORY_SEPARATOR, $ Item ); $ filename = str_replace ( '/', DIRECTORY_SEPARATOR, $ filename ); } // var_dump ($ Item); // var_dump (the file_exists ($ Item)); Die; IF ( the file_exists ( $ Item )) { $ ZIP -> the addFile ( $ Item , the basename ( $ Item )); } } $ ZIP -> use Close (); IF (! file_exists ( $ filename {)) Exit ( "Can not find file"); // create even still possible to fail } header ( 'Content-Type: the Application / ZIP' ); header('Content-disposition: attachment; filename='.basename($filename)); header('Content-Length: ' . filesize($filename)); @readfile($filename);
     
@unlink ( $filename );
}

Note: All paths inside the absolute path, or will not find the file

Comes with other actions:

Unzip the zip file

public  function unzip_file ( $ File , $ dir ) { 

        // instantiate an object 

        $ zip = new new ZipArchive (); 

        // open the zip file, if you open a failure return message 

        IF ( $ zip -> Open ( $ File ) ==! TRUE ) { 

          Die ( "Could not Open Archive" ); 

        } 

        // extract the compressed files to the specified directory 

        $ zip -> extractTo ( $ dir ); 

        // close the zip archive 

        $ zip -> use Close ();   

    }

Get-extracting file directory

public  function loopFun ( $ dir )   

    {   

        $ handle = opendir ( $ dir . "." ); 

        // define the arrays to store the file name 

        $ array_file = Array (); 

        the while ( false ! == ( $ File = readdir ( handle $ ))) 

        { 

            IF ( $ file ! "." && = $ file ! = ".." ) { 

                $ array_file [] = $ the dir .. '/' $ file ; // output filename 

            }

        }

        closedir($handle);

        return $array_file;

        //print_r($array_file);

    }

 

Guess you like

Origin www.cnblogs.com/zhangdong156/p/11771174.html