Force download multiple blob files in php

时光总嘲笑我的痴心妄想 提交于 2019-12-08 07:04:37

问题


I want to add a method to my platform that allows users to download all the files from an application, that will be zipped-up. Each application consists of multiple files that are stored in separate rows of the files-table in the database.

I tried many options, but just can't seem to add the actual files to the zip.

This is my code so far:

    $sql = "SELECT * FROM files WHERE applications_id = :applications_id"; 
    $stmt = $db->prepare($sql); 
    $stmt->bindParam(':applications_id', $applications_id, PDO::PARAM_INT); 
    $stmt->execute(); 
    $items = $stmt->fetchAll();

    $zipfilename = 'temp_' . time() . '.zip';
    $zip = new ZipArchive; 
    $zip->open($zipfilename, ZipArchive::CREATE); 
    foreach ($items as $item) { 
     //$zip->addFromString($item['filename'],$item['filename']);
    } 
    $zip->close(); 

    header('Content-Type: application/zip'); 
    header('Content-disposition: attachment; filename='.$zipfilename); 
    header('Content-Length: ' . filesize($zipfilename)); 
    readfile($zipfilename); 

    exit();

Any help would be greatly appreciated!

来源:https://stackoverflow.com/questions/34205913/force-download-multiple-blob-files-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!