I am working on a button which will download the images as a zip on specific posts. It works if I leave the files
array in functions.php
with a few hard-coded img
links. But when I move the array to the custom post where the images I need to zip are already being looped and displayed, I can still download via button, but opening the downloaded zip I get error "Unable to expand archive. It is in an unsupported format"
I have tried moving different parts of the code back and forth, like all of the code in the custom post, and all of the code in functions.php
, to no avail. I must be not getting the img
file url
s properly but I'm not sure how.
This is all of the mentioned code, only in functions.php
(except the download button), with the hard-coded image links, which works as intended:
$image1 = 'https://com.tripletigers/wp-content/uploads/homesweet_nowords-1920x1920.jpg';
$image2 = 'https://com.tripletigers/wp-content/uploads/V3-1920x2880.jpg';
$image3 = 'https://com.tripletigers/wp-content/uploads/SM_Single-Cover_Damn-Strait-1920x1920.jpg';
$files = array($image1, $image2, $image3);
$tmpFile = tempnam('/tmp', '');
$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
// download file
$fileContent = file_get_contents($file);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
// Download Created Zip file from button via form submit
if(isset($_POST['download'])){
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=artist-photos.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
}
This is what leads to the error, trying to get the images from the otherwise working loop in the custom post:
foreach( $list_posts as $list_item ) :
if ( $list_item->post_type == 'crb_photo' ):
if ( has_post_thumbnail( $list_item->ID ) ) {
$img_url = get_the_post_thumbnail_url( $list_item->ID, 'crb_full_width' );
// this var i created for the zip, to add each image url to the array
$getImage = esc_url($img_url);
// testing: prints image url. Why is that url not getting to the zip?
echo '<h6>' . $img_url . '</h6>';
// create array for zip - tried both = and .=
$files[] .= $getImage;
// same result as above
// $files[] = $getImage;
} ?>
<li>
<div>
<?php if ( ! empty( $img_url ) ) : ?>
<a href="<?php echo esc_url( $img_url ); ?>"></a>
<?php endif; ?>
</div>
</li>
<?php endif; ?>
The functions.php
used with above (same as first snippet but without hard-coded array)
$tmpFile = tempnam('/tmp', '');
$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
// download file
$fileContent = file_get_contents($file);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
// Download Created Zip file
if(isset($_POST['download'])){
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=artist-photos.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
}
When trying to open zip via command line:
What am i missing in order to get those images into the zip properly?
file_get_contents
to download the thumbnail URLs, why not access the file directly off the disk? Useget_post_thumbnail_id
to get the attachment ID then use the standard attachment functions to grab the path/filename of the file for the image size you wantaddFile( $file, basename( $file ) )
instead of reading it in as a string and usingaddFromString
?filename=artist-photos.zip')
tofilename=artist-photos-2.zip')
, and so on. Hopefully that would have prevented what you mentioned. I'm just getting back to this but will update my question code later with where I'm at with it..