duanhao8540
duanhao8540
2014-07-29 08:48

PHP没有创建Zip

  • ziparchive
  • php
  • wordpress
已采纳

I am trying to create a zipped folder to store all the images from the wordpress database. My images are being fetched from database, but unable to create zipped file. I read on Stack Overflow that php do not create zip from http:\\ then i did it using absolute path, still no luck :( . I have tried following code to generate Zip

add_action("gform_after_submission_89", "export_triphub_2_zip", 10, 2);
function export_triphub_2_zip($entry, $form)
{
    global $wpdb,$current_user;
    get_currentuserinfo();
    $uid = $current_user->ID;
    $photos = array();
    $path = getcwd();
   foreach($entry as $item)
   {

        if($item != '')
        {
              if($item == 'Photos')
              {
                 $photos[] = $wpdb->get_results("select photo_path from wp_triphub_photos where userid=".$uid);
                 $photos   = $photos[0];
                 foreach($photos as $photo)
                 {
                    if($photo->photo_path != '')
                    {
                       $files[] = $path.'/dev.guestseed.com'.$photo->photo_path;
                    }
                 }
              }
              elseif($item == 'Hotel')
              {
                 $photos1[] = $wpdb->get_results("select room_photo from wp_triphub_hotel_room_info where userid=".$uid);
                 $photos1   = $photos1[0];
                 foreach($photos1 as $photo1)
                 {
                    if($photo1->room_photo != '')
                    {
                       $files[] = $path.'/dev.guestseed.com'.$photo1->room_photo;
                    }
                 }
              }
              elseif($item == 'Activity')
              {
                 $photos2[] = $wpdb->get_results("select activity_photo from wp_triphub_activity_info where userid=".$uid);
                 $photos2   = $photos2[0];
                 foreach($photos2 as $photo2)
                 {
                    if($photo2->activity_photo != '')
                    {
                       $files[] = $path.'/dev.guestseed.com'.$photo2->activity_photo;
                    }
                 }
              }
              elseif($item == 'People')
              {
                 $photos3[] = $wpdb->get_results("select person_pic from wp_triphub_people_photos where userid=".$uid);
                 $photos3   = $photos3[0];
                 foreach($photos3 as $photo3)
                 {
                    if($photo3->person_pic != '')
                    {
                       $files[] = $path.'/dev.guestseed.com'.$photo3->person_pic;
                    }
                 }
              }
        }

    }

    echo "<pre>";
      print_r($files); //Files are being fetched properly
    echo "</pre>";

    $zipname = 'file.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();


}

Here is print_r result

Array
(
    [0] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Pigs-Swim1.jpg
    [1] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Pigs-Swim.jpg
    [2] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/04/Koala6.jpg
    [3] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/04/Lighthouse15.jpg
    [4] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Board-Kids-Gift-Cards-v21.jpg
    [5] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Shark-Swim-v1.jpg
    [6] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/AIRPORT1-final1.jpg
    [7] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Lighthouse4.jpg
    [8] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Lighthouse5.jpg
    [9] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/nobu-final.jpg
    [10] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/New-Deal-1-final1.jpg
    [11] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/AIRPORT1-final.jpg
    [12] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Lighthouse6.jpg
    [13] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-75a83e9dd9d095f5db638b2d9da64ea/2014/07/Facebook_NYC_Getaway_Ad_v3.jpg
    [14] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/2-975a83e9dd9d095f5db638b2d9da64ea/2014/07/Facebook_NYC_Getaway_Ad_v31.jpg
    [15] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/19-5d374fb89356ce253d8063b139b80aa7/2014/06/00005n.jpg
    [16] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/19-5d374fb89356ce253d8063b139b80aa7/2014/07/Books_WEB.jpg
    [17] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/26-c188840623d940ffe93d9175b45f99fd/2014/04/Hydrangeas1.jpg
    [18] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/26-c188840623d940ffe93d9175b45f99fd/2014/07/Bahamas_Swim_With_Dolphins-v2.jpg
    [19] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/07/AIRPORT1-final2.jpg
    [20] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/07/nobu-final1-final.jpg
    [21] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/03/limo.JPG
    [22] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/03/Theater-picture1.jpg
    [23] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/07/nobu-final1-final1.jpg
    [24] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/07/nobu-final1-final2.jpg
    [25] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/07/nobu-final1-final3.jpg
    [26] => /mnt/target06/844844/847596/dev.guestseed.com/web/content/dev.guestseed.com/wp-content/uploads/gravity_forms/11-d181d4592d89de424b652145502fe9bc/2014/04/Lighthouse6.jpg
)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douchiwan1503 douchiwan1503 7年前

    First of all, according to our conversation below, change back the PATH to use http://, after it use this code for downloading images to a compressed folder.

    $zip = new ZipArchive(); 
    $tmp_file = tempnam('.',''); 
    $zip->open($tmp_file, ZipArchive::CREATE); 
        foreach($files as $file){ 
            $download_file = file_get_contents($file); 
        $zip->addFromString(basename($file),$download_file); 
        $zip->close(); 
    header('Content-disposition: attachment; filename=download.zip'); 
    header('Content-type: application/zip'); 
    readfile($tmp_file); 
    

    Hope that helps,

    点赞 评论 复制链接分享
  • duangu8264 duangu8264 7年前

    Two advices from php.net

    1. If you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist. Apparently $zip->addFile() returns TRUE even if the file doesn't actually exist. It's a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.

    2.Don't forget to check the zip isn't empty, folks - otherwise the zip won't be created at all, and the server will issue no warning!

    点赞 评论 复制链接分享