I've created a zip file named 'files.zip'. I have images inside my property_image folder. I've scanned that directory and have the type of files I need. '$prop_image' is another array which have the names of images. If an image file(which is in the variable '$file1') is in that array then I'm trying add the file into zip file.
$zip = new ZipArchive();
$filename = "files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>
");
}
$dir = './property_image';
$file1 = scandir($dir);
foreach($file1 as $feel){
$userfile_extn = substr($feel, strrpos($feel, '.')+1);
if($userfile_extn === 'jpg' || $userfile_extn === 'png' || $userfile_extn === 'gif' || $userfile_extn === 'jpeg'){
if(in_array($feel, $prop_image)){
$zip->addFile("$feel", basename($feel));
}
} // end of if($extension ===
}
$zip->addFile("$File", basename($File));
$zip->close();
This code only adds '$File' into that zip file if I comment out the code:
$zip->addFile("$feel", basename($feel));
I need to add those files from that directory which are inside the variable '$prop_image' in the zip file.
How can I do that?
I solved it.
It might be helpful to someone else so, I'm posting my answer. That was easy. I only need to give the folder name in which the images are saved.
Like this::
$zip->addFile("./property_image/$feel", basename($feel));
in place of::
$zip->addFile("$feel", basename($feel));