php创建zip文件(来自post附件wordpress) [英] Php create zip file (from post attachments wordpress)
问题描述
我正在尝试从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();
两个方法均未返回任何内容. 任何见解将不胜感激.
编辑我-数组$ 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-这是简短的答案.请参见下面的长篇文章)
好的-我将在这里回答我自己的问题..
我已经证实自己的怀疑-通过HTTP传递时,PHP无法创建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 )
(感谢@DaveRandom关于查看var_dump数组的评论-实际上我已经看了很多遍了,但是直到有人特别要求看到它时,我才注意到了很多.)
这让我想起了很久以前gdlib的另一个问题-有关PHP流函数,创建文件和HTTP的问题. 例如,像gdlib或pdf动态创建之类的图像库,它们都在HTTP上失败.
I am trying to create a zip file from post attachments in wordpress .
I have tried both methods below - but it results in nothing (No error messages , no file created) - What am I doing wrong (again .. )
I do not think that The fact that those are post attachments in wordpress has anything to do with it - because those methods failed also with normal files . why ? --> See the answer .
$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();
and also this method :
$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();
Both methods returned nothing .. Any insights will be appreciated .
EDIT I - sample 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 )
..by using get_attached_file() it will produce real path ( at some point I suspected that maybe php can not create zip over HTTP - and that Is the short answer . See the long one below.)
Ok - I will answer my own question here ..
I have confirmed my own suspicions - PHP can not create ZIP when passed over HTTP - so we need a PATH and not a URL ...
So for example in Wordpress case, need to use get_attached_file() to produce real path ..
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 )
(Thanks @DaveRandom for his comment about seeing the var_dump array -I had actually looked at it many times, but until someone specifically asked to see it I did not pay much attention.)
Then it made me remember another problem I had long time ago with gdlib - about PHP stream functions , creating files - and HTTP. For example image libraries like gdlib, or pdf dynamic creation they all fail on HTTP.
这篇关于php创建zip文件(来自post附件wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!