Hallo
Voor een website wil ik graag de mogelijkheid creƫren om een map (met afbeeldingen) te downloaden als zip. Nu heb ik hiervoor al wat gevonden op deze site: travis berry
<?php
// WARNING
// This code should NOT be used as is. It is vulnerable to path traversal. https://www.owasp.org/index.php/Path_Traversal
// You should sanitize $_GET['directtozip']
// For tips to get started see http://stackoverflow.com/questions/4205141/preventing-directory-traversal-in-php-but-allowing-paths
//Get the directory to zip
$filename_no_ext= $_GET['directtozip'];
// we deliver a zip file
header("Content-Type: archive/zip");
// filename for the browser to save the zip file
header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
// get a tmp name for the .zip
$tmp_zip = tempnam ("tmp", "tempname") . ".zip";
//change directory so the zip file doesnt have a tree structure in it.
chdir('user_uploads/'.$_GET['directtozip']);
// zip the stuff (dir and all in there) into the tmp_zip file
exec('zip '.$tmp_zip.' *');
// calc the length of the zip. it is needed for the progress bar of the browser
$filesize = filesize($tmp_zip);
header("Content-Length: $filesize");
// deliver the zip file
$fp = fopen("$tmp_zip","r");
echo fpassthru($fp);
// clean up the tmp zip file
unlink($tmp_zip);
?>
Met als link naar dit script:
<a href="zip_folders.php?directtozip=img">Download All As Zip</a>
Het downloaden lukt, maar wanneer ik het bestand open is de melding: Kan bestand img niet openen, dit archief is mogelijk beschadigd.
Heeft iemand een idee waaraan dit liggen kan, en hoe ik het op zou kunnen lossen?
bvd,
groeten Wouter
2.093 views