I create zip file from directory like this :
<?php // On instancie la classe.
$zip = new ZipArchive();
if($zip->open($cheminAlbum.'.zip', ZipArchive::CREATE) == TRUE)
{
$fichiers = scandir($cheminAlbum.'/');
// On enlève . et .. qui représentent le dossier courant et le dossier parent.
unset($fichiers[0], $fichiers[1]);
foreach($fichiers as $f) {
if(!$zip->addFile($cheminAlbum.'/'.$f, $f)) {
echo 'Impossible d'ajouter "'.$f.'".<br/>';
} else {
echo 'Fichier ajouter " : '.$f.'".<br/>';
}
}
$zip->close();
} else {
echo 'Erreur, impossible de créer l'archive.';
}
?>
I add link for download it like this :
<a href="<?php echo $cheminAlbum.'.zip';?>"> Download Album </a>
When i access to directory, i dont found the zip file after call php page And when i click to the link, the file doesnt exist
The file is add correctly to the zip when i debug (add echo)
what i forgot in y code ?
The Problem is with the file name. your code is creating a foldername/.zip file instead of foldername/album1.zip check the below code add a dynamic filename in url and when opending
when creating and providing access to files you need to consider two things. when creating file you need to use File System path and when you are providing link you need to provide url of the file
<?php // On instancie la classe.
$zip = new ZipArchive();
$filename = "somealbumName";
if($zip->open($cheminAlbum.'/'.$filename.'.zip', ZipArchive::CREATE) == TRUE)
{
$fichiers = scandir($cheminAlbum.'/');
// On enlève . et .. qui représentent le dossier courant et le dossier parent.
unset($fichiers[0], $fichiers[1]);
foreach($fichiers as $f) {
if(!$zip->addFile($cheminAlbum.'/'.$f, $f)) {
echo 'Impossible d'ajouter "'.$f.'".<br/>';
} else {
echo 'Fichier ajouter " : '.$f.'".<br/>';
}
}
$zip->close();
} else {
echo 'Erreur, impossible de créer l'archive.';
}
?>
<a href="<?php echo $cheminAlbum.'/'.$filename.'.zip';?>"> Download Album </a>
Unless it's defined in another file you haven't shown, $cheminAlbum would have no value. So, your anchor leads nowhere. Have you tried adding debug statements such as printing values of variables with print_r()? Are you sure you have your variables defined and the files they are defined in included if not in the file you are accessing them from?
I used this function, the zip is created well in the folder. I intend that after downloading, the zip is deleted from the server
<?php // On instancie la classe.
$zip = new ZipArchive();
$filename = "$nom_ref";
$cheminAlbum = "../../../../Images-Produits/2018/$nom_ref";
if($zip->open($cheminAlbum.'/'.$filename.'.zip', ZipArchive::CREATE) == TRUE)
{
$fichiers = scandir($cheminAlbum.'/');
// On enlève . et .. qui représentent le dossier courant et le dossier parent.
unset($fichiers[0], $fichiers[1]);
foreach($fichiers as $f) {
if(!$zip->addFile($cheminAlbum.'/'.$f, $f)) {
//echo 'Impossible d'ajouter "'.$f.'".<br/>';
} else {
//echo 'Fichier ajouter " : '.$f.'".<br/>';
}
}
$zip->close();
} else {
//echo 'Erreur, impossible de créer l'archive.';
}
?>
<a href="<?php echo $cheminAlbum.'/'.$filename.'.zip';?>" action="action" class="btn btn-dropbox" title="Télécharger <?php echo $nom_ref; ?>.zip" style="float: left;">
<i class="fa fa-download"></i> TÉLÉCHARGER .ZIP</a>