-1

I am working on my personal wordpress site that distribute images.

In single post page, I registered several images via ACF(Advanced Custom Fields) and I know how to get image path/filename through get_field ACF function.

I just googled then found this page, but how can I apply this technique to wordpress site? Download multiple images into zip

Now I am stuck...

Community
  • 1
  • 1
  • Questions asking us to go somewhere else to try to figure out what you're asking are not appropriate here. If you have issues with code, post the **relevant** code here, in the post itself, and ask a specific question related to that code. See [ask] and [mcve]. – Ken White Feb 22 '17 at 04:06
  • You need to explain exactly where you are stuck. – Joe Niland Feb 22 '17 at 04:12
  • can you please provide your custom field code for retrieving the images, i just only want to check either they are in repeater image sub-field or different image custom field, so that i can provide you the required code for it. – Prateek Verma Feb 22 '17 at 09:41
  • And please also provide details if all those media images are stored in 'uploads' folder or in any other folder. – Prateek Verma Feb 22 '17 at 09:52

1 Answers1

1

On single post page, place the url of download_zip.php file, where you will place all your code for creating zip.

On single post page:

<a href="<?php echo site_url().'/download_zip.php?model_id='.$post->ID; ?>">Download ZIP</a>

In variable 'model_id', place the post id of the single post.

Now create a download_zip.php file on the root of your wordpress setup, where wp-config.php file exists.

Here is the code of download_zip.php file.

<?php 
/*File for downloading the gallery zip files*/
$post_id = $_GET['model_id'];
require_once('wp-blog-header.php');
require_once('/wp-admin/includes/file.php');
WP_Filesystem();
$files_to_zip = array();
$zip = new ZipArchive();
$title = get_the_title($post_id); 
$destination = wp_upload_dir(); 
//var_dump($destination); 
$destination_path = $destination['basedir'];
$DelFilePath = str_replace(" ","_",$title).'_'.$post_id.'_'.time().'.zip' ;
$zip_destination_path = $destination_path."/".$DelFilePath;
if(file_exists($destination_path."/".$DelFilePath)) {
    unlink ($destination_path."/".$DelFilePath); 
}
 if ($zip->open($destination_path."/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
        die ("Could not open archive");
}

//this is only for retrieving Repeater Image custom field
$row1 = get_field('acf_field_name1',$post_id);
$row1 = get_field('acf_field_name2',$post_id);
$rows = array($row1,$row2);
if($rows) {
foreach($rows as $row): ?>
    <?php  
    $explode = end(explode("uploads",$row));
    $index_file = array($destination_path,$explode);
    $index_file = implode("",$index_file);
    $new_index_file = basename($index_file);
    $zip->addFile($index_file,$new_index_file);
endforeach; 
}
$zip->close();
if(file_exists($zip_destination_path)) {
    send_download($zip_destination_path);
}

//The function with example headers
function send_download($file) {
    $basename = basename($file);
    $length   = sprintf("%u",filesize($file));
    header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
    header('Content-Description: File Transfer');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.$basename.'"');
    header('Content-Transfer-Encoding: binary');
    header('Pragma: public');
    header('Content-Length: ' . $length);
    set_time_limit(0);
    ob_clean();
    flush();
    readfile($file); // "Outputs" the file.
    unlink($file);
}

?>

Please modify this code according to your requirement such as get_field(), place your image field name inside it, & define your upload directory, so that you can break the url in $explode variable for defining path of image in $index_file variable.

And please also check your destination path stored in $destination_path variable is correct or not.

Hope, this may be helpful to you.

Prateek Verma
  • 869
  • 1
  • 6
  • 9
  • Thanks for the tip! I'll try and let u know if it works for me! – Taro Gotanda Feb 23 '17 at 05:32
  • 1
    It's bad practice to add files to WordPress core. Better method will be to place the download.php in themes root folder and call it with https://developer.wordpress.org/reference/functions/get_template_directory_uri/ instead of site_url – mondrey Sep 19 '17 at 02:13