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

时间:2014-05-15 02:32:08

标签: php wordpress download ziparchive

我正在尝试创建用户上传图片的zip文件,当用户点击按钮时,该文件将生成下载。该按钮位于Wordpress管理员的页面上。该页面来自添加新管理部分的插件。单击该按钮时,它会将表单提交到'themes / twentyfourteen / page-templates'目录中的页面。该页面名为cartoon_zip.php,是通过admin中的Pages部分创建的,因此它的网址为http://www.sitename.com/cartoon-zip/。当我单击该文件时,它似乎工作。不会发生错误,并显示保存文件对话框。我在本地保存文件并尝试解压缩,但得到消息'文件的内容“characters.zip”无法用此程序提取。“我在Mac上,所以它是The Unarchiver试图打开;但它在PC上也不起作用。下面是我正在使用的代码;非常感谢任何帮助。

<?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/>";
    }
?>

编辑:因此我想通过下面的doublesharp组合帮助以及将标题更改为以下内容来实现此目的。

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

这是完整的工作代码。

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

答案 0 :(得分:1)

addFile()的第一个参数需要是图像的完整路径,但是您使用的是wp-content/...的相对路径。此页面的模板很可能位于wp-content/themes/目录中,因此对file_exists()的调用失败,这意味着没有任何内容传递给ZipArchive。 WordPress提供了一个常量,其中包含名为WP_CONTENT_DIRwp-content文件夹的绝对路径 - 或者如果您不确定将使用哪个主题,则可以使用get_template_directory()

使用WP_CONTENT_DIR

if( ! empty( $image ) ){
    $filename = WP_CONTENT_DIR.'/themes/twentyfourteen/upload/character/'.$image;
    if ( file_exists( $filename ) ){
        // Adding files into zip
        $zip->addFile( $filename, $image );
    }
}

使用get_template_directory()

$filename = get_template_directory().'/upload/character/'.$image;

SQL INJECTION WARNING

您还有一个SQL注入漏洞,可以在请求的order_id中传递任意SQL。使用$wpdb->prepare()来防范它。

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