1

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!

  • http://php.net/manual/en/pdo.error-handling.php --- http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Dec 10 '15 at 15:51
  • I've never done this in PDO, but I think you need to use `PARAM_LOB` for BLOBs http://php.net/manual/en/pdo.lobs.php – Funk Forty Niner Dec 10 '15 at 15:56
  • I don't seem to get any errors, I just haven't found a way to add multiple blobs. Single ones already work without zipping. – Henk Braber Dec 10 '15 at 15:58
  • See this answer (it's mysql_ though) http://stackoverflow.com/a/31655346/ you need a `while( $row = mysql_fetch_assoc( $ImageCollect)){...}` so in your case that would be something like `PDO::FETCH_ASSOC`. I hope that helps. – Funk Forty Niner Dec 10 '15 at 16:05
  • The problem is that my files (images/text/pdf) are in de database itself. Therefore there's no actual path to the file. Well I guess there is, but that's what I'm missing. – Henk Braber Dec 10 '15 at 18:13

0 Answers0