Obm*_*nen 5 php wordpress zipfile ziparchive
我正在尝试从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();
Run Code Online (Sandbox Code Playgroud)
还有这个方法:
$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();
Run Code Online (Sandbox Code Playgroud)
两种方法都没有返回任何内容.任何见解都将受到赞赏
编辑I - 示例print_r for array $ files_to_zip
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 )
Run Code Online (Sandbox Code Playgroud)
..by使用get_attached_file()它会产生真正的路径(在某些时候我怀疑也许php无法通过HTTP创建zip - 这是简短的答案.请参阅下面的长篇文章.)
好的 - 我会在这里回答我自己的问题..
我已经证实了自己的怀疑 - PHP在通过HTTP传递时无法创建ZIP - 所以我们需要一个PATH而不是一个URL ......
所以例如在Wordpress的情况下,需要使用get_attached_file()来生成实际路径.
Array (
[0] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-62316IMAG0659.jpg
[2] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IGP0255.jpg
[3] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IZTP0635.jpg
[4] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_ITG035t5.jpg
[5] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IRTT7375.jpg )
Run Code Online (Sandbox Code Playgroud)
(感谢@DaveRandom关于看到var_dump数组的评论 - 我实际看了很多次,但是直到有人特别要求看到它我才引起注意.)
然后它让我记住了我很久以前使用gdlib的另一个问题 - 关于PHP流函数,创建文件和HTTP.例如像gdlib或pdf动态创建的图像库,它们都在HTTP上失败.
| 归档时间: |
|
| 查看次数: |
4415 次 |
| 最近记录: |