PHP从多个文件夹中压缩和下载文件

时间:2013-07-03 03:06:57

标签: php download directory zip

所以我在做一个localhost网站的字体,我设法让用户选择多个文件到购物车,如果它们来自同一个文件夹,例如C:/ fonts / a或C,则可以下载: / fonts / b但如果a和b都有文件,则无法下载

那么可以这样做吗?比如从a和b

下载所选文件

以下是我的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是c:\ fonts \
a只是\因为php只是在我写的时候给了我问题 Font_Family是c:\ fonts \
中不同的文件夹名称 所以路径就像C:\ fonts(这里的文件夹名称)\

1 个答案:

答案 0 :(得分:0)

您可以使用每种字体,无论目录如何,并将其复制到临时目录,例如C:/fonts/temp/cartID/。然后,您可以压缩该字体目录(一些来自/ fonts / a,一些来自/ fonts / b)并提供下载。然后,您需要进行垃圾收集,即删除临时cartID目录。

只是我的2¢

相关问题