PHP没有创建Zip

时间:2014-07-29 08:48:51

标签: php wordpress ziparchive

我正在尝试创建一个压缩文件夹来存储wordpress数据库中的所有图像。我的图像是从数据库中提取的,但无法创建压缩文件。我在Stack Overflow上读到php不从http:\\创建zip然后我用绝对路径做了它,仍然没有运气:(。我试过以下代码来生成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();


}

以下是print_r结果

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 个答案:

答案 0 :(得分:1)

来自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!

答案 1 :(得分:1)

首先,根据下面的对话,在使用此代码将图像下载到压缩文件夹后,将PATH更改为使用http://。

$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); 

希望有所帮助,