I am trying to create a zip file from email attachments in wordpress.
I tried using both methods below - but it gives nothing (no error messages, no file is created) - What am I doing wrong (again ..)
I donβt think that the fact that these messages are attachments to wordpress has anything to do with it - since these methods also do not work with regular files. why? β See answer.
$files_to_zip = array();
$post_id = get_the_id();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$files_to_zip [] = wp_get_attachment_url( $attachment->ID );
}
}
print_r($files_to_zip);
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files_to_zip as $file) {
$zip->addFile($file);
}
$zip->close();
as well as this method:
$files_to_zip = array();
$post_id = get_the_id();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post_id
);
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$zip->addFile(wp_get_attachment_url( $attachment->ID ));
}
}
print_r($files_to_zip);
$zip->close();
Both methods did not return anything. Any ideas would be appreciated.
EDIT I - an example of an array print_r $ files_to_zip
print_r($files_to_zip);
Array (
[0] => http:
[2] => http:
[3] => http:
[4] => http:
[5] => http:
.. get_attached_file() real ( - , , , php zip HTTP - ..)