2017-02-22 52 views
-2

我正在我個人的wordpress網站上發佈圖片。在Wordpress中下載zip文件中的多個圖像

在單篇文章頁面中,我通過ACF(高級自定義字段)註冊了幾個圖像,並且我知道如何通過get_field ACF函數獲取圖像路徑/文件名。

我剛剛搜索了一下,然後找到了這個頁面,但是我怎麼能把這個技術應用到wordpress站點呢? Download multiple images into zip

現在我堅持......

+0

問題要求我們去別的地方試圖找出你要問的東西在這裏不合適。如果您在使用代碼時遇到問題,請在此處發佈相關的**代碼,然後提出與該代碼相關的具體問題。見[問]和[mcve]。 –

+0

您需要準確解釋卡住的位置。 –

+0

您能否請您提供您的自定義字段代碼來檢索圖像,我只是想檢查他們是否在中繼器圖像子字段或不同的圖像自定義字段,以便我可以爲您提供所需的代碼。 –

回答

0

在單篇文章頁面,將download_zip.php文件,將放置所有的代碼創建ZIP的URL。

在單後頁:

<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裏面,&定義上傳目錄,這樣就可以在$explode變量打破url$index_file變量定義圖像的路徑。

並請檢查您的destination path存儲在$destination_path變量是否正確。

希望,這可能對你有所幫助。

+0

感謝您的提示!我會盡力讓你知道它是否適合我! –

+0

將文件添加到WordPress核心是個不好的做法。更好的方法是將download.php放在主題根文件夾中,並使用https://developer.wordpress.org/reference/functions/get_template_directory_uri/而不是site_url來調用它 – user2685224