I am trying to make a list of files from a directory have them shown in a form via checkboxes. When the user checks files they want to download have them zipped and downloaded. I have basically no experience in php but have managed to make my list with checkboxes and a download script attached to the button that when it is selected should start the download but doesn't it does however clear the boxes.If possible I would also like to store a copy of this zip to a folder on the server in case the user would like to re-download in a certain amount of time. here is the code I have. Someone let me know where I went wrong and please explain any corrections to code as I am trying to learn as I do this little project.
<?php
$error = ""; //error holder
if(isset($_POST['createzip'])){
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip')){ // Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}else
$error .= "* Please select file to zip <br/>";
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>
Here's checkbox
<?php
if ($handle = opendir('./')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo '<input type="checkbox" name="files[]" value=".$entry"/> '.$entry. '<br />';
}
}
closedir($handle);
?>
Aucun commentaire:
Enregistrer un commentaire