As in the image above ,I have images that are organized in virtual folders(in
If the "files" you want to add to your zip directory are stored in a db as strings, you could use ZipArchive::addFromString. If the db stores pointers to actual files use ZipArchive::addFile
Thanks @dnagirl for your answer, but I was actually looking for a way to add files inside subfolders (maybe I was unable to make myself clear!)
discovered that zipArchive automatically creates folder if we give 'path/to/filename
' instead of just 'filename
'
This is how I achieved it in case anybody may need it!
function zipFilesAndDownload($file_names,$archive_file_name,$img_localpath)
{
$folder_path=WSP_AK_PLUGIN_DIR ."akTempFolder/";
$zip = new ZipArchive();
if ($zip->open($folder_path.$archive_file_name, ZIPARCHIVE::CREATE )===TRUE) {
//$zip->addEmptyDir('myimages');(this was what i thought would create folder!)
foreach($file_names as $files)
{
//$zip->addFile($img_localpath.'/'.$files, 'myfolder/'.$files);
$zip->addFile($img_localpath.'/'.$files['fileName'], $files['folder_path'].$files['fileName']);
}
$zip->close();
in above code $files['folder_path']
will give the path to file where it will be created For eg folder1/folder2/image1.jpg
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($folder_path.$archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
if( file_exists($folder_path.$archive_file_name)) {
ob_clean();
readfile($folder_path.$archive_file_name);
}
unlink($folder_path.$archive_file_name);
}
}