File: /home/imensosw/public_html/imenso.co/matrix/admin/download_excel.php
<?php
error_reporting(0);
// ini_set(‘display_errors’, 1);
// ini_set(‘display_startup_errors’, 1);
// error_reporting(E_ALL);
require_once '../config.php';
// include('../PHPExcel/Classes/PHPExcel/IOFactory.php');
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$doc_no = $_POST['list'][0];
if(isset($doc_no))
{
$doc_no = explode(',', $doc_no);
$error = "";
$filePath = "../docs/";
if(extension_loaded('zip'))
{
if(isset($doc_no) && count($doc_no) == 1)
{
$file = $doc_no[0];
// $objPHPExcel = new PHPExcel();
// $FileName = "../docs/$file/$file.xls" ;
$FileName1 = $filePath.$file.'/'.$file.'.xls';
$FileName2 = $filePath.$file.'/'.$file.'.xlsx';
$ext = '';
if(file_exists($FileName1))
{
$ext = pathinfo($FileName1, PATHINFO_EXTENSION);
}
else if(file_exists($FileName2)){
$ext = pathinfo($FileName2, PATHINFO_EXTENSION);
}
if($ext!='')
{
$inputFileName = "../docs/$file/$file.".$ext ;
// $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
// $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
// $objPHPExcel->setActiveSheetIndex(0);
// header("Content-Type: application/vnd.ms-excel");
// header("Content-Disposition: attachment; filename=$file.".$ext);
// header("Cache-Control: max-age=0 ");
// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
// $objWriter->save('php://output');
// ob_clean();
ob_end_clean(); // this is solution
header('Content-Description: File Transfer');
// header('Content-Type: ' . $mime);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($inputFileName) . "\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($inputFileName);
}
}
else if(isset($doc_no) && count($doc_no)>1)
{
$zipPath = "../zip_docs/";
if (!file_exists($zipPath)) {
mkdir($zipPath, 0777, true);
}
array_map('unlink', glob( $zipPath.'*.zip'));
$zip = new ZipArchive();
$zip_name = date("Y-m-d").".zip";
if($zip->open($zipPath.$zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
//$error.="Sorry zip creation failed.";
}
$array = array();
foreach($doc_no as $file)
{
$FileName1 = $filePath.$file.'/'.$file.'.xls';
$FileName2 = $filePath.$file.'/'.$file.'.xlsx';
if(file_exists($FileName1))
{
$ext = pathinfo($FileName1, PATHINFO_EXTENSION);
$array[] = $FileName1;
$inputFileName = $ext;
$zip->addFile($filePath."$file/$file.".$inputFileName,"$file.".$inputFileName);
}
else if(file_exists($FileName2)){
$ext = pathinfo($FileName2, PATHINFO_EXTENSION);
$array[] = $FileName2;
$inputFileName = $ext;
$zip->addFile($filePath."$file/$file.".$inputFileName,"$file.".$inputFileName);
}
// $zip->addFile($filePath."$file/$file.".$inputFileName,"$file.".$inputFileName);
}
$zip->close();
$yourfile = $zipPath.$zip_name;
if(file_exists($yourfile))
{
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
unlink($yourfile);
}
else
{
//$error .= "File not exists ";
}
}
else
{
//$error .= "* Please select file to zip ";
}
}
/*$_SESSION['file_error'] = $error;
header("location:".$url."admin/assignment_completed.php");*/
}
?>