Im using Joomla 3.x as my website content manager and also using the NoNumber sourcerer extension which allows me to place PHP code anywhere in my Articles. Im using ZipArchive to zip selected files and download them in a .zip. However, when downloaded the zip file becomes corrupted because when viewed in a text editor you can see all of the HTML surrounding the article from the Joomla Framework. Is there anyway around this??? Ive been struggling with this for a while now and any help would be appreciated.
Here is what the end of what is placed between the sourcerer tags:
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 Explorer just tell me its empty and trying toextract tells me it invalid and 7zip can get whats inside but the actual .xlsx files are corrupter and it cant do anything with them.
This is what is produced from a sample file when looking at it in a text editor:
<!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()
etc... Followed by all the binary which is all I want followed by more joomla html footers and (You get the idea)
So how do ppl make zip files in joomla and include the headers for them to be downloaded and not corrupted by this?
Thanks for any help