joomla ZipArchive生成无效的zip文件

时间:2015-07-09 22:12:40

标签: php joomla zip ziparchive

我使用Joomla 3.x作为我的网站内容管理器,并使用NoNumber sourcerer扩展,它允许我将PHP代码放在我的文章中的任何位置。我使用ZipArchive压缩选定的文件并以.zip格式下载。但是,下载时zip文件已损坏,因为在文本编辑器中查看时,您可以看到Joomla Framework中文章周围的所有HTML。有没有办法解决???我一直在努力解决这个问题,任何帮助都会受到赞赏。

以下是sourcerer标记之间的结尾:

require_once("../php/Archive/Zip.php");

if((!empty($_POST['showdate']) || isset($_POST['showdate'])) && (!empty($_POST['showsheets']) || isset($_POST['showsheets'])) )
{
    // echo $_POST['showdate'];
    // foreach($_POST['showsheets'] as $showsheet)
    // {
        // echo $showsheet;
    // } 

    $files = $_POST['showsheets'];
    unset($_POST['showsheets']);
    $zippath = 'jdownloads/tmp/';
    $zipname = $_POST['showdate'].'ShowSheets.zip';
    unset($_POST['showdate']);    

    //$regex = "/^Jan | ^June | ^Sept/";
    foreach( new DirectoryIterator($zippath) as $fileInfo ){
        if( $fileInfo->isDot() || !$fileInfo->isFile() || $fileInfo->getFilename() == 'index.html' ) 
            continue;           
     //   if (preg_match($regex, $fileInfo->getFilename())) {  
        unlink($fileInfo->getPathname());
      //  }
    }

    $zip = new ZipArchive;
    $zip->open($zippath . $zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
        //$content = file_get_contents($file);
        //$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);
    }   
    $zip->close();

    // header('Content-Type: application/zip');
    // header('Content-disposition: attachment; filename='.$zipname);
    // header('Content-Length: ' . filesize($zippath . $zipname));
    // readfile($zippath . $zipname);

    // http headers for zip downloads
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$zipname."\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($zippath . $zipname));
    //ob_end_flush();
    readfile($zippath . $zipname);
}
?>

Windows资源管理器告诉我它是空的并且尝试toextract告诉我它无效,7zip可以在里面找到什么但实际的.xlsx文件是腐败的,它不能对它们做任何事情。

这是在文本编辑器中查看示例文件时生成的内容:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <base href="http://wheatbeltusa.com/index.php/testing-one" />
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="author" content="Super User" />
  <meta name="description" content="Farm and Home Supplies" />
  <meta name="generator" content="Joomla! - Open Source Content Management" />
  <title>Testing One - Wheatbelt Inc.</title>
  <link href="http://wheatbeltusa.com/index.php/testing-one" rel="canonical" />
  <link href="/templates/protostar/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
  <link rel="stylesheet" href="/templates/protostar/css/template.css" type="text/css" />
  <link rel="stylesheet" href="/media/system/css/frontediting.css" type="text/css" />
  <script src="/media/jui/js/jquery.min.js" type="text/javascript"></script>
  <script src="/media/jui/js/jquery-noconflict.js" type="text/javascript"></script>
  <script src="/media/jui/js/jquery-migrate.min.js" type="text/javascript"></script>
  <script src="/media/system/js/caption.js" type="text/javascript"></script>
  <script src="/media/jui/js/bootstrap.min.js" type="text/javascript"></script>
  <script src="/media/system/js/frontediting.js" type="text/javascript"></script>
  <script type="text/javascript">
jQuery(window).on('load',  function() {
                new JCaption('img.caption');
            });
jQuery(document).ready(function(){
    jQuery('.hasTooltip').tooltip({"html": true,"container": "body"});
});
jQuery(document).ready(function()

等...跟随所有二进制文件,这是我想要的更多joomla html页脚和(你明白了)

那么ppl如何在joomla中创建zip文件并包含要下载的头文件而不会被这个文件损坏?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

OMG,

我刚刚剥离了决赛?&gt;并添加一个出口;在readfile之后,它就会逃脱它。如:

ob_clean();
flush();

readfile($filepath);

exit;
}