J'utilise la classe ZipArchive de PHP pour créer un fichier zip contenant des photos et l'envoyer au navigateur pour téléchargement. Voici mon code :
/**
* Grabs the order, packages the files, and serves them up for download.
*
* @param string $intEntryID
* @return void
* @author Jesse Bunch
*/
public static function download_order_by_entry_id($intUniqueID) {
$objCustomer = PhotoCustomer::get_customer_by_unique_id($intUniqueID);
if ($objCustomer):
if (!class_exists('ZipArchive')):
trigger_error('ZipArchive Class does not exist', E_USER_ERROR);
endif;
$objZip = new ZipArchive();
$strZipFilename = sprintf('%s/application/tmp/%s-%s.zip', $_SERVER['DOCUMENT_ROOT'], $objCustomer->getEntryID(), time());
if ($objZip->open($strZipFilename, ZIPARCHIVE::CREATE) !== TRUE):
trigger_error('Unable to create zip archive', E_USER_ERROR);
endif;
foreach($objCustomer->arrPhotosRequested as $objPhoto):
$filename = PhotoCart::replace_ee_file_dir_in_string($objPhoto->strHighRes);
$objZip->addFile($filename,sprintf('/press_photos/%s-%s', $objPhoto->getEntryID(), basename($filename)));
endforeach;
$objZip->close();
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($strZipFilename)).' GMT', TRUE, 200);
header('Cache-Control: no-cache', TRUE);
header('Pragma: Public', TRUE);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT', TRUE);
header('Content-Length: '.filesize($strZipFilename), TRUE);
header('Content-disposition: attachment; filename=press_photos.zip', TRUE);
header('Content-Type: application/octet-stream', TRUE);
ob_start();
readfile($strZipFilename);
ob_end_flush();
exit;
else:
trigger_error('Invalid Customer', E_USER_ERROR);
endif;
}
Ce code fonctionne très bien avec tous les navigateurs sauf IE. Dans IE, le fichier se télécharge correctement, mais l'archive zip est vide. Lorsque j'essaie d'extraire les fichiers, Windows me dit que l'archive zip est corrompue. Quelqu'un a-t-il déjà rencontré ce problème ?
Modifier Mise à jour : Après une suggestion de @profitphp, j'ai changé mes en-têtes en ceci :
header("Cache-Control: public");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
//header("Content-Description: File Transfer");
//header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"pressphotos.zip\"");
//header("Content-Transfer-Encoding: binary");
header("Content-length: " . filesize($strZipFilename));
Voici également une capture d'écran de l'erreur dans Windows après ouverture avec Firefox :
Cette erreur se produit à la fois dans IE et Firefox sous Windows. Elle fonctionne bien sur Mac. De plus, sous Windows, la taille des fichiers semble correcte :
Edit #2 Ce problème est résolu. Voir ma réponse ci-dessous.