表格一遍又一遍地下载相同的ZIP

时间:2011-12-23 19:16:44

标签: php

感谢此论坛上的用户社区,我编写了一个非常简单的Web表单,允许我的用户从他们的Internet浏览器中查看文本文件。

我现在有两个函数,搜索返回的文本文件被压缩成ZIP。这是我的代码

    function getFilesFromSite() { 
     $result = null; 
     $ZIPresult = null;
     if (empty($_POST['DBSite'])) { return null; } 
     $mydir = MYDIR;  
     $dir = opendir($mydir); 
     $DBSite = $_POST['DBSite']; 
     $getfilename = mysql_query("select filename from search_table where site='" . $DBSite . "'") or die(mysql_error()); 
     while ($row = mysql_fetch_array($getfilename)) { 
     $filename = $row['filename']; 
     $result .= '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>'; 
     $ZIPresult .= basename($mydir) . '/' . $filename.' ';
    }

    if ($result) {
    $result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>";
    shell_exec("/bin/rm -f SearchResult.zip;/usr/bin/zip -9 SearchResult.zip ". $ZIPresult ." > /dev/null ");

    //header for forced download
    header("Pragma: public");
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $fileName = 'SearchResult.zip';
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Transfer-Encoding: binary");
    header('Content-type: application/zip');
    header("Content-length: " . filesize($fileName));
    header('Content-Disposition: attachment; filename="' . $fileName . '"');

    ob_start(); // Starts output buffering.
    readfile($fileName); // "Outputs" the file.
    $content = ob_get_flush(); // Grabs the output and assigns it to a variable.
    print base64_encode($content);
    }

    function getFilesFromError() { 
//Just a copy paste from above with different input parameter...
    }

问题在于,首先完成任何搜索内容的ZIP文件会一次又一次地下载。例如,即使我之后使用getFilesFromError()进行了搜索,getFilesFromSite()的结果也会一直下载。

我怀疑我的标题设置不正确,但我不知道在哪里。

PS:新的ZipArchive()库/类在我们的生产环境中不可用,所以我选择使用Unix实用程序ZIP。

1 个答案:

答案 0 :(得分:0)

由于此处陈述的原因,使用Base64实际上无法正常工作。相反,我关闭zlib压缩并恢复使用二进制作为输出格式。最后,我在标题中将Content-Type设置为application/octet-stream。现在一切都很好;这是我的代码:

function getFiles() {
     ini_set('zlib.output_compression', 'Off');  
     $result = null;
     $ZIPresult = null;
     $cleanup = null;
     $output = null;
     $fileName = null;
    //remove old zip if any
     $cleanup = shell_exec("/bin/rm -f SearchResult.zip");
     error_log("SHELL OUTPUT=>" . $cleanup, 0);
     //test
     if (empty($_POST['DBRIDs'])) { return null; }
     $mydir = MYDIR; // set from the CONSTANT 
     $dir = opendir($mydir);
     $DBRIDs = $_POST['DBRIDs'];
     $getfilename = mysql_query("select /*! SQL_CACHE */ filename from automation where rid in (" . $DBRIDs . ")") or die(mysql_error());
     while ($row = mysql_fetch_array($getfilename)) {
     $filename = $row['filename'];
     $result .= '<tr><td><a href="' . basename($mydir) . '/' . $filename . '" target="_blank">' . $filename . '</a></td></tr>';
     $ZIPresult .= basename($mydir) . '/' . $filename.' ';
    }

    if ($result) {
    $result = "<table><tbody><tr><td>Search Results.</td></tr> $result</table>";
    $output = shell_exec("/usr/bin/zip SearchResult.zip ". $ZIPresult ." ");
    error_log("SHELL OUTPUT=>" . $output, 0);
    $fileName = 'SearchResult.zip';
    error_log("ZIP FILENAME=>" . $fileName, 0);

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

    return $result;
    }

感谢所有人抽出时间!!