dongque4778 2014-05-15 02:32
浏览 414
已采纳

由ZipArchive生成的PHP动态生成的Zip文件无法打开

I'm trying to create a zip file of user uploaded images that will be generated for download when a user clicks a button. The button is on a page in the Wordpress admin. The page is from a plugin that adds a new admin section. When the button is clicked, it submits a form to a page in 'themes/twentyfourteen/page-templates' directory. The page is called cartoon_zip.php and was created through the Pages section in the admin, so it has the url of http://www.sitename.com/cartoon-zip/. When I click the file, it appears to work. No errors occur, and a save file dialog appears. I save the file locally and try to unzip but get the message 'The contents of the file "characters.zip" can not be extracted with this program.' I'm on a mac, so it's The Unarchiver trying to open; it doesn't work on a PC either though. Below is the code I'm using; any help is greatly appreciated.

<?php 
    /*
        Template name: Cartoon Zip
    */

    if(isset($_REQUEST['order_id'])){
        $order_id = $_REQUEST['order_id'];

        $query = "SELECT optional_image FROM order_listing WHERE order_no = '".$order_id."'";
        $query2 = "SELECT main_image FROM character_order WHERE order_id = '".$order_id."'";

        $entries = $wpdb->get_results($query);
        $entries2 = $wpdb->get_results($query2);

        $error = "";        //error holder

        if(extension_loaded('zip')){    // Checking ZIP extension is available

            $zip = new ZipArchive();            // Load zip library 
            $zip_name = "characters.zip";           // Zip name

            if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
                $error .=  "* Sorry ZIP creation failed at this time<br/>";
            }

            foreach( $entries as $entry ) {
                $image=$entry->optional_image; 
                if(!empty($image)):
                    $url = 'wp-content/themes/twentyfourteen/upload/main/'.$image;
                    if(file_exists($url)){
                        $zip->addFile($url,$image);         // Adding files into zip
                    }
                endif;
            }
            foreach( $entries2 as $entry ) {
                $image=$entry->main_image; 
                if(!empty($image)):
                    $url = 'wp-content/themes/twentyfourteen/upload/character/'.$image;
                    if(file_exists($url)){
                        $zip->addFile($url,$image);         // Adding files into zip
                    }
                endif;
            }

            if ($zip->close() === false) {
                exit("Error creating ZIP file");
            };

            $filename = $zip_name;
            // send $filename to browser
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mimeType = finfo_file($finfo, $filename);
            $size = filesize($filename);
            $name = basename($filename);

            if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
                // cache settings for IE6 on HTTPS
                header('Cache-Control: max-age=120');
                header('Pragma: public');
            } else {
                header('Cache-Control: private, max-age=120, must-revalidate');
                header("Pragma: no-cache");
            }
            header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // long ago
            header("Content-Type: $mimeType");
            header('Content-Disposition: attachment; filename="' . $name . '";');
            header("Accept-Ranges: bytes");
            header('Content-Length: ' . filesize($filename));

            readfile($fullFilePath);
            exit;
        }else
            $error .= "* You dont have ZIP extension<br/>";
    }
?>

EDIT: So I manged to get this working with a combination doublesharp's help below and by changing the headers to the following.

$file = $zip_name;
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }

Here's the full working code.

<?php 
/*
    Template name: Cartoon Zip
*/

if(isset($_REQUEST['order_id'])){
    global $wpdb;
    $order_id = isset( $_REQUEST['order_id'] )? $_REQUEST['order_id'] : 0;

    $query = $wpdb->prepare( "SELECT optional_image FROM order_listing WHERE order_no = %s", $order_id );
    $query2 = $wpdb->prepare( "SELECT main_image FROM character_order WHERE order_id = %s", $order_id );

    $entries = $wpdb->get_results($query);
    $entries2 = $wpdb->get_results($query2);

    $error = "";        //error holder

    if(extension_loaded('zip')){    // Checking ZIP extension is available

        $zip = new ZipArchive();            // Load zip library 
        $zip_name = "characters.zip";           // Zip name

        if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
            $error .=  "* Sorry ZIP creation failed at this time<br/>";
        }

        foreach( $entries as $entry ) {
            $image=$entry->optional_image; 
            if( ! empty( $image ) ){
                $filename = WP_CONTENT_DIR.'/themes/twentyfourteen/upload/main/'.$image;
                if ( file_exists( $filename ) ){
                    // Adding files into zip
                    $zip->addFile( $filename, $image );
                }
            }
        }
        foreach( $entries2 as $entry ) {
            $image=$entry->main_image; 
            if( ! empty( $image ) ){
                $filename = WP_CONTENT_DIR.'/themes/twentyfourteen/upload/character/'.$image;
                if ( file_exists( $filename ) ){
                    // Adding files into zip
                    $zip->addFile( $filename, $image );
                }
            }
        }

        if ($zip->close() === false) {
            exit("Error creating ZIP file");
        };

        $file = $zip_name;
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }else
        $error .= "* You dont have ZIP extension<br/>";
}
?>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 求十字路口交通灯S7-200plc,今天早上八点前。
      • ¥20 如何用智能手表oppowatch3实现个人热点功能
      • ¥15 运营商打开wanpush会有怎么样的危害
      • ¥15 Java编程题,定义一个时钟
      • ¥15 Python面向对象编程——生成多项式的一阶导数
      • ¥15 在编写C++程序遇到了问题
      • ¥15 java程序查找字符串中所有的回文并输出
      • ¥15 怎么让只支持对.exe 和.dll文件进行加壳的加壳器,同时让它也支持php文件加壳呢
      • ¥15 这两个数学公式什么意思?
      • ¥15 求助大佬:如何解决char类型多维数组的多次输入问题?