【问题标题】:PHP .zip file download error when opening in windows explorer在 Windows 资源管理器中打开时 PHP .zip 文件下载错误
【发布时间】:2014-03-20 18:57:14
【问题描述】:

我在尝试打开 PHP 时遇到了一个奇怪的错误,在 Windows 资源管理器中创建了 .zip 文件。

在 WinRar 中运行良好。

我正在创建的是一个 SDS 表下载站点,您可以在其中选中您想要的 PDF 文档类型,然后将它们压缩。

该过程运行良好,我在创建文件时没有错误。只有在使用 Windows 资源管理器打开时才会出现错误。

错误信息: “Windows 无法打开该文件夹。压缩(压缩)文件夹‘文件名’无效。”在 Windows 资源管理器中打开时出错。”

如果您想自己测试,该网站是http://www.premiere-produkter.no/pp/datablad/index.php

代码如下:

<?php
    $error = "";        //error holder
    if(isset($_POST['createpdf'])){
        $post = $_POST;     
        $file_folder = "files/";    // folder to load files
        if(extension_loaded('zip')){    // Checking ZIP extension is available
            if(isset($post['files']) and count($post['files']) > 0){    // Checking files are selected
                $zip = new ZipArchive();            // Load zip library 
                $zip_name = time().".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($post['files'] as $file){               
                    $zip->addFile($file_folder.$file);          // Adding files into zip
                }
                $zip->close();
                if(file_exists($zip_name)){
                    // push to download the zip
                    header('Content-type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                    readfile($zip_name);
                    // remove zip file is exists in temp path
                    unlink($zip_name);
                }

            }else
                $error .= "* Please select file to zip <br/>";
        }else
            $error .= "* You dont have ZIP extension<br/>";
    }
?>

HTML:

    <body>    
    <div id="container">
    <div id="meny">
    <h2><a href="http://www.premiere-produkter.no"><img src="files/pp.png">Gå til Premiere Produkter sine sider ved å klikke her </a> </h2>

    <h1>last ned datablad / Download SDS sheets</h1>
    <form name="zips" method="post">
    <?php if(!empty($error)) { ?>
    <p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; margin:0 auto 10px;"><?php echo $error; ?></p>
    <?php } ?>
    <h5>
    <p>Vennligst Marker de databladene du ønsker å få tak i, og så trykk på "last ned til zip" etterpå for å laste ned en komprimert zip fil der alle databladene ligger i. </p>
    <p>Du kan også se på pdf filene ved å trykke på navnet.</p> 
    <p>Sliter du med å finne produktet? Bruk søkefeltet til å søke etter enten navn eller produktnummer.
    </h5>
    <table class="tablesorter" id="tblSearch" width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid; background:#fff;">
      <thead>
      <tr>
        <th  align="center"><span style="font-size:13px;">Marker de du vil laste ned</span></th>
        <th align="center">File Type</th>
        <th>Fil Navn</th>
        <th>Språk</th>
        <th>Faresymboler</th>
        <th>Art.Nr</th>
      </tr>
      </thead>

        <tr>
        <td align="center"><input type="checkbox" name="files[]" value="11001-11081.pdf" /></td>
        <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
        <td><a href="files/11001-11081.pdf">Savona D2</a></td>
        <td align="center" alt="nok"><span style="font-size:0px;">no</span><img src="norge.jpg" alt="Nor" title="nor" width="20" height="20" /></td>
        <td align="center"><img src="/images/faresymboler/irriterende.png" title="pdf" width="20" height="20" /></td>
        <td>11001-11081</td>
      </tr>
    </table>

<div id="buttons">
<div class="b1">
<input type="submit" value="Last ned som ZIP fil" style="border:0px; margin:10px 0; 

background-color:#800040; color:#FFF; padding:7px; width:234px; cursor:pointer; font-

weight:bold; border-radius:0px;" name="createpdf">
</div>
<div class="b1">
<input type="reset" value="Reset" style="border:0px; background-color:#D3D3D3; color:#000; 

font-weight:bold; width:235px; padding:10px; cursor:pointer; border-radius:0px;" 

name="reset">
</div>

【问题讨论】:

  • 您已经在 ZIP 文件的末尾附加了您网站的 HTML,不是吗?
  • 是的,如果有帮助,可以添加部分代码,这是一个很长的代码,所以不会将其全部添加为大量重复的代码段。
  • 现在添加了 HTML 部分。

标签: php download zip windows-explorer winrar


【解决方案1】:

添加

ob_clean();
ob_end_flush();

之前

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);

注意:添加此代码以在将 zip 内容发送到浏览器之前清除所有 html 输出

【讨论】:

    【解决方案2】:

    我会详细说明我的评论。您的 ZIP 文件包含有效的 ZIP 内容以及最后的完整 HTML 文档。我并不是说你在最后添加了正确压缩的 HTML——它只是附加了:

    无法从您共享的代码中说明这是如何发生的,但一旦您意识到这一点,就应该很明显地修复它。


    教育猜测 - 我想你有这个:

    if(file_exists($zip_name)){
        // push to download the zip
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
        readfile($zip_name);
        // remove zip file is exists in temp path
        unlink($zip_name);
    }
    ?>
    <!DOCTYPE html>
    <head>
    ...
    

    例如,您可以在运行不需要的代码之前完成脚本:

    unlink($zip_name);
    exit;
    

    ...或使用if() 构造不运行它:

    if(file_exists($zip_name)){
        // ...
    }else{ ?>
        <!DOCTYPE html>
        <head>
    <? }
    

    【讨论】:

    • 哇,你说得对,现在在记事本中检查了文件,你建议我如何解决这个问题?
    • 问题是你添加了额外的输出。解决方法是不这样做。 ;-)
    • exit 为我做到了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多