Auf einzelne Post-Seite, legen Sie die URL download_zip.php
Datei, wo Sie alle Ihre Code für die Erstellung von Zip platzieren wird.
Auf einzige Beitrag Seite:
<a href="<?php echo site_url().'/download_zip.php?model_id='.$post->ID; ?>">Download ZIP</a>
In Variable 'model_id'
, legen Sie die Post-ID des einzelnen Beitrags.
Erstellen Sie nun eine download_zip.php
Datei im Stammverzeichnis Ihres Wordpress-Setups, in dem die Datei wp-config.php existiert.
Hier ist der Code download_zip.php
Datei.
<?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);
}
?>
Bitte diesen Code ändern entsprechend Ihrer Anforderung wie get_field()
, platzieren Sie Ihre image field name
hinein, & Upload-Verzeichnis definieren, so dass Sie die url
in $explode
Variable zur Definition Pfad des Bildes in $index_file
Variable brechen kann.
Und bitte überprüfen Sie auch Ihre destination path
gespeichert in $destination_path
Variable ist korrekt oder nicht.
Hoffe, dies kann für Sie hilfreich sein.
Fragen, die uns bitten, woanders hinzugehen, um herauszufinden, was Sie fragen, sind hier nicht angebracht. Wenn Sie Probleme mit dem Code haben, posten Sie den ** relevanten ** Code hier, im Post selbst und stellen Sie eine spezifische Frage zu diesem Code. Siehe [fragen] und [mcve]. –
Sie müssen genau erklären, wo Sie stecken bleiben. –
können Sie bitte Ihren benutzerdefinierten Feld Code zum Abrufen der Bilder, ich möchte nur nur entweder im Repeater-Bild-Unterfeld oder andere Bild benutzerdefinierte Feld, so dass ich Ihnen den erforderlichen Code dafür zur Verfügung stellen kann. –