damsean102
9/15/2015 - 11:38 AM

Group files and Zip them

Group files and Zip them

/*DROP THIS INTO FUNCTIONS PHP*/
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files['path'],$files['name']);
    }
    $zip->close();
    //then send the headers to foce download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}

/*THEN ON YOUR TEMPLATE*/
$file_path=$_SERVER['DOCUMENT_ROOT'].'/victoria-albert-baths/wp-content/uploads/';

/*If Repeater Get files*/
$files = get_field('product_downloads');

foreach($files as $file){
    
	$filemeta = wp_get_attachment_url($file['file_upload']);
	$filemeta = explode('/',$filemeta);
					
	$fileInfo[] = array(
		'name' => $filemeta[8],
		'path' => $filemeta[6].'/'.$filemeta[7].'/'.$filemeta[8]
	);
}

zipFilesAndDownload($fileInfo,$downloadname,$file_path);
exit;