0

I have the following image gallery in ACF:

$slider = get_field( 'slider' );

I need to create a function so that with a single click, the entire image gallery is turned over to a .zip file. Anyone have something similar? I've found some things, but for some reason it doesn't work. Thanks.

EDIT:

I have tried to implement this:

<?php 
if ( $slider ) :
    $destination = 'downloads/' . sanitize_title( get_the_title() ) . '.zip';
    if ( file_exists( $destination ) ) {
        echo '<a href="' . esc_url( home_url( '/' ) ) . $destination . '" class="download-link" download>DOWNLOAD ALL</a>';
    } else {
        $files = array();
        foreach ( $slider as $singlefile ) {
            $files[] = get_attached_file( $singlefile['ID'] );
        }

        if ( count( $files ) ) {
            $zip = new ZipArchive();
            $zip->open( $destination, ZipArchive::CREATE );

            foreach ( $files as $file ) {

                if ( file_exists( $file ) ) {
                    $new_filename = substr( $file, strrpos( $file, '/' ) + 1 );
                    $zip->addFile( $file, $new_filename );
                }
            }
            $zip->close();

            echo '<a href="' . esc_url( home_url( '/' ) ) . $destination . '" class="download-link" download>DOWNLOAD ALL</a>';
        } else {
            echo 'no files found';
        }
    }
endif;
?>

But when the file is downloaded, I receive a download error and I don't know what I am doing wrong.

2
  • Have you got anything as a starting point for us to help you with? Written any code/solutions? A quick google provides this: tring-web-design.co.uk/2017/04/…
    – Ben H
    Commented Nov 21, 2019 at 16:24
  • @BenH Hi, yes sorry, I have already placed it up.
    – Dario B.
    Commented Nov 25, 2019 at 8:32

1 Answer 1

0

I'm using this same code and I was able to figure out where the error's happening. The path that gets generated when this line of code is called is wrong:

$zip->open( $destination, ZipArchive::CREATE );

It spits out your home directory and then appends the value of $destination to it.

In my case (it's not the cleanest solution, but it's the one I ended up doing), I hard-coded the destination instead of using the variable.

$zip->open( 'wp-content/themes/my theme/downloads/' . sanitize_title( get_the_title() ) . '.zip', ZipArchive::CREATE );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.