【发布时间】:2012-03-28 11:19:46
【问题描述】:
我正在尝试从 wordpress 中的帖子附件创建一个 zip 文件。
我已经尝试了以下两种方法 - 但它没有任何结果(没有错误消息,没有创建文件) - 我做错了什么(再次......)
我不认为那些是 wordpress 中的帖子附件的事实与它有任何关系 - 因为这些方法对于普通文件也失败了。为什么? --> 看看答案。
$files_to_zip = array();// create files array
//run a query
$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 ); // populate files array
}
}
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();
还有这个方法:
$files_to_zip = array(); // create array
//run a query
$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);// debug - return file names OK
$zip->close();
这两种方法都没有返回任何内容.. 任何见解将不胜感激。
EDIT I - 数组 $files_to_zip 的示例 print_r
print_r($files_to_zip);
Array (
[0] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-62316IMAG0659.jpg
[2] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IGP0255.jpg
[3] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IZTP0635.jpg
[4] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_ITG035t5.jpg
[5] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IRTT7375.jpg )
..通过使用 get_attached_file() 它将产生 real 路径(在某些时候我怀疑也许 php 无法通过 HTTP 创建 zip - 这就是简短的答案。请参阅下面的长篇.)
【问题讨论】:
-
没有称为
create_zip()的原生 PHP 函数 - 请显示此函数的代码。另外,请显示var_dump($files_to_zip)的结果 -
查看我对示例数组的编辑。
标签: php wordpress zipfile ziparchive