简体   繁体   中英

PHP Zip and download files from multiple folder

So I am doing a localhost website for fonts, and I managed to let the user select multiple files into a cart, it can be download if they are from the same folder for example either C:/fonts/a or C:/fonts/b but if there are files from both a and b, it can't be downloaded

So is it possible to do that? like download selected files from both a and b

Below is my code for download.php

            <?php
            include 'dbfunctions.php';

            if (!$link) {
                die(mysqli_error($link));
            }
            $sql="SELECT id, Font_Name, Font_Family
                        FROM font";
            $result = mysqli_query($link, $sql) or die(mysqli_error($link));

            while($row = mysqli_fetch_array($result)){

            $Font_Family = $row['Font_Family'];
            $a = file_get_contents('./a.txt');
            $file_dir = file_get_contents('./font_path.ini');

            $path = $file_dir.$Font_Family.$a;

            $error = ""; //error holder
            if(isset($_POST['createpdf']))
            {
            $Font_Family = $_POST['Font_Family'];
            }
            $file_folder = $path;// folder to load files
            if(extension_loaded('zip'))
            {
            // Checking ZIP extension is available
            if(isset($_POST['files']) and count($_POST['files']) > 0)
            {

            // Checking files are selected
            $zip = new ZipArchive(); // Load zip library
            $zip_name = time().".zip"; // Zip name
            if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
            {
             // Opening zip file to load files
            $error .= "* Sorry ZIP creation failed at this time";
            }
            foreach($_POST['files'] as $file)
            {
            $zip->addFile($file_folder.$file,pathinfo($file,PATHINFO_BASENAME)); 
            readfile($path);

            }
            $zip->close();
            if(file_exists($zip_name))
            {
            // push to download the zip
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$zip_name.'"');
            readfile($zip_name);
            // remove zip file is exists in temp path
            unlink($zip_name);
            }

            }
            else
            $error .= "* Please select file to zip ";
            }
            else
            $error .= "* You dont have ZIP extension";
            }

            ?>

fontpath.ini is c:\\fonts\\
a is just \\ since php gives me problem when I just write \\
Font_Family is the different folder name inside c:\\fonts\\
so the path is like C:\\fonts(whichever folder name here)\\

You could take each font, regardless of the directory, and copy it to a temp dir, say, like, C:/fonts/temp/cartID/ . You could then zip that directory of fonts (some from /fonts/a and some from /fonts/b) and provide the download. You then would need to garbage collect, that is, delete the temp cartID dir.

Just my 2¢

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM