如何使用php创建多个ZIP存档

时间:2014-04-16 19:33:25

标签: php zip

我想使用PHP创建多个zip文件夹。

然后将主题添加到一个ZIP文件夹中。

如果用户选择

-Album 1
-Album 2

点击下载按钮

我的脚本应创建album1.zipalbum2.zip,然后将它们放入一个ZIP文件download.zip

这是我的代码,它只创建一个zip文件专辑。 但我想为多张专辑做这件事

 <?php
 include 'db.php';

    if(isset($_POST['songs']) > 0) {
    $songs = $_POST['songs'];
    $urls = Array();
    $number = rand(8454622, 9845622189);
    foreach($songs as $song) {
        $q = mysql_query("SELECT * FROM songs WHERE song_id = '$song'");
            while($r = mysql_fetch_array($q)) {
                $path = $r['song_path'];
                array_push($urls, $path);
            }
    }

            // set Random File names 
            $filename = "zips/mol-zip-".$number;
            // Call the Class ZipArchive();
            $zip = new zipArchive();
            // Now Lets Open / Create the Zip File ...
            $zip->open($filename.'.zip', zipArchive::CREATE);
            // Get the Files to Add them into Zip Archive ........
            foreach($urls as $url) {    
            // exploade the File name from URL
            $get_name = explode("/" ,$url);
            $song_name = end($get_name);

                 $zip->addFile($url, "songs/{$song_name}");
            }
            $zip->close();
            // Now Read This created zip file .....
            $file = $filename.'.zip';
            if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
                unlink($file);
                }

        }
        else {
            echo "No Songs Selected";
        }
            ?> 

0 个答案:

没有答案