0

I have a form from which, we get the data from users. Images are stored as blob in database. Now, I want all of the responded images to my local storage (computer).

<?php
include 'config.php';
session_start();
$table_name = $_SESSION["table"];

$zip = new ZipArchive();
$ZipFileName = $table_name . '.zip';

if ($zip->open($ZipFileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
    echo "Cannot Open";
}

$zip->addEmptyDir('newFolder');
$query = mysqli_query($con, "SELECT * FROM $table_name");  

while($row = mysqli_fetch_assoc($query)) {
    $temp = $row['student_id'] . '.jpg';
    $zip->addFromString($temp,  $row['photo']);
}

$zip->close();

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=' . $ZipFileName);
header("Content-Transfer-Encoding: binary"); 
header('Expires: 0');
header('Pragma: no-cache');
header("Content-length: " . filesize($ZipFileName));
?>

Zip file is downloaded, but at the time of extraction it shows error. When I tried to download only one image it shows no preview. How can I download all the blob files as images to my computer (a zip file)?

  • You set all of the headers, but don't actually output the zip file(https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) is is this just missing from your code shown here? – Nigel Ren May 24 '21 at 06:07
  • Oh, I forgot that, silly mistake. Thankyou so much Nigel Ren – Yashashree Patel May 24 '21 at 06:28

0 Answers0