遍历图像列表并添加到 zip 下载问题
Looping through list of images and adding to zip download issue
我有以下代码,基本上抓取用户“65”添加的所有 Wordpress 图片。然后我尝试压缩所有这些图片并下载。
我遇到的问题是它只压缩 foreach 中的最后一张图片。
我不知道哪里出了问题,正在寻求专家指导。
非常感谢。
# get all attachments (images) for user ID 65
$pictures = new WP_Query( array( 'author' => '65', 'post_type' => 'attachment', 'post_status' => 'inheret', 'posts_per_page' => -1 ) );
if ( $pictures->posts ) :
foreach ( $pictures->posts as $picture ) :
if (wp_get_attachment_image_url($picture->ID, 'full')) :
$files = array(wp_get_attachment_image_url( $picture->ID, 'full' ));
if ($files) :
//echo $files;
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
ob_clean();
# send the file to the browser as a download
$datestamp = date("d-M-Y", time());
header('Content-disposition: attachment; filename=Backup-65-'.$datestamp.'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
endif;
endif;
endforeach;
endif;
您将在每次迭代时创建一个新的 ZipArchive
实例。将除添加文件外的所有操作移到循环外。
# get all attachments (images) for user ID 65
$pictures = new WP_Query( array( 'author' => '65', 'post_type' => 'attachment', 'post_status' => 'inheret', 'posts_per_page' => -1 ) );
if ( $pictures->posts ) :
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach ( $pictures->posts as $picture ) :
$file = wp_get_attachment_image_url($picture->ID, 'full');
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
endforeach;
$zip->close();
ob_clean();
# send the file to the browser as a download
$datestamp = date("d-M-Y", time());
header('Content-disposition: attachment; filename=Backup-65-'.$datestamp.'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
endif;
我有以下代码,基本上抓取用户“65”添加的所有 Wordpress 图片。然后我尝试压缩所有这些图片并下载。
我遇到的问题是它只压缩 foreach 中的最后一张图片。
我不知道哪里出了问题,正在寻求专家指导。
非常感谢。
# get all attachments (images) for user ID 65
$pictures = new WP_Query( array( 'author' => '65', 'post_type' => 'attachment', 'post_status' => 'inheret', 'posts_per_page' => -1 ) );
if ( $pictures->posts ) :
foreach ( $pictures->posts as $picture ) :
if (wp_get_attachment_image_url($picture->ID, 'full')) :
$files = array(wp_get_attachment_image_url( $picture->ID, 'full' ));
if ($files) :
//echo $files;
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
ob_clean();
# send the file to the browser as a download
$datestamp = date("d-M-Y", time());
header('Content-disposition: attachment; filename=Backup-65-'.$datestamp.'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
endif;
endif;
endforeach;
endif;
您将在每次迭代时创建一个新的 ZipArchive
实例。将除添加文件外的所有操作移到循环外。
# get all attachments (images) for user ID 65
$pictures = new WP_Query( array( 'author' => '65', 'post_type' => 'attachment', 'post_status' => 'inheret', 'posts_per_page' => -1 ) );
if ( $pictures->posts ) :
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach ( $pictures->posts as $picture ) :
$file = wp_get_attachment_image_url($picture->ID, 'full');
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
endforeach;
$zip->close();
ob_clean();
# send the file to the browser as a download
$datestamp = date("d-M-Y", time());
header('Content-disposition: attachment; filename=Backup-65-'.$datestamp.'.zip');
header('Content-type: application/zip');
readfile($tmp_file);
endif;