admin管理员组文章数量:1395151
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.
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.
Share Improve this question edited Nov 25, 2019 at 8:31 Dario B. asked Nov 21, 2019 at 13:54 Dario B.Dario B. 15510 bronze badges 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
Reset to default 0I'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 );
本文标签: phpACF Gallery field images donwload
版权声明:本文标题:php - ACF Gallery field images donwload 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744747446a2622970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论