我正在处理分发图片的个人wordpress网站。
在单个帖子页面中,我通过ACF(高级自定义字段)注册了几个图像,我知道如何通过get_field
ACF功能获取图像路径/文件名。
我只是用谷歌搜索然后找到了这个页面,但是如何将这种技术应用到wordpress网站? Download multiple images into zip
现在我被卡住了......
答案 0 :(得分:1)
在单个帖子页面上,放置download_zip.php
文件的网址,您将放置所有代码以创建zip。
在单个帖子页面:
<a href="<?php echo site_url().'/download_zip.php?model_id='.$post->ID; ?>">Download ZIP</a>
在变量'model_id'
中,放置单个帖子的帖子ID。
现在在wordpress设置的根目录下创建一个download_zip.php
文件,其中存在wp-config.php文件。
以下是download_zip.php
文件的代码。
<?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);
}
?>
请根据您的要求修改此代码,例如get_field()
,将image field name
放入其中,&amp;定义您的上传目录,这样您就可以打破url
变量中的$explode
来定义$index_file
变量中的图像路径。
另请检查destination path
变量中存储的$destination_path
是否正确。
希望,这可能对您有所帮助。