Php create zip file (from post attachments wor

2019-02-13 21:24发布

问题:

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.)

回答1:

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.