<?php
session_start();
error_reporting(0);
$un = $_SESSION['username'];
require_once("../model/conn.php");
require_once '../function/npLog.php';
$report_dir = $_SERVER["DOCUMENT_ROOT"]."/report/FILES/";
$operation = $_GET['h'] == 1 ? "delete" : "download";
if($operation == "download") // 0
{
$n = $_GET['n'];
if($n == 1)
{
$type = "single";
$format = "html";
}
else if($n == 2)
{
$type = "single";
$format = "pdf";
}
else if($n == 3)
{
$type = "all";
$format = "html";
}
else if($n == 4)
{
$type = "all";
$format = "pdf";
}
if($type == "single")
{
$filename = $_GET[filename];
if (!file_exists($report_dir.$filename))//查看报表文件是否存在
{
echo "can't find file";
exit;
}
if($format == "html")
{
$files = array($filename);
download_html_report_files($report_dir.$filename, $files);
}
else if($format == "pdf")
{
download_pdf_files($report_dir.$filename);
}
}
else if($type == "all")
{
$file_path = $report_dir;
set_time_limit(0);
list($input_start_time,$input_end_time) = explode("_",$_REQUEST[startToend]);
$start_time = date("Ymd",strtotime("$input_start_time"));
$end_time = date("Ymd",strtotime("$input_end_time"));
if($format == "pdf")
{
download_files($file_path,$start_time, $end_time,$format);
}
else if($format == "html")
{
download_files($file_path,$start_time,$end_time ,$format);
}
}
}
else if($operation == "delete")
{
$dir = $_SERVER["DOCUMENT_ROOT"].'/report/FILES/';
$dir1 = $_SERVER["DOCUMENT_ROOT"].'/report/IMG/';
$filename=$_GET['filename'];
if(!file_exists($dir.$filename ))
{
echo "can't find file";
exit;
}
else
{
$re2="删除报表".$filename."成功!";
$myAudit['audit_type'] = "'报表管理'";
$myAudit['usr_name'] = "'".$un."'";
$myAudit['client_ip'] = "'".getenv('REMOTE_ADDR')."'";
$myAudit['action_result'] = 1;
$myAudit['action_desc'] = "' 系统管理员 ".$un.$re2." ' ";
np_log($myAudit);
$imgfilename2=substr($filename,-3,-1);
if($imgfilename2=='pd')
{
unlink($dir . $filename);
header("Location: ./FILES/index.php");
}
else
{
$imgfilename=substr($filename,0,-5);
$files = scandir($dir1, 1);
foreach ($files as $index => $filename1)
{
$filename2=substr($filename1,0,strlen($imgfilename));
if ($imgfilename == $filename2)
{
$imgstring.=$dir1.$filename1." ";
}
}
exec("rm -rf $imgstring");
unlink($dir.$filename);
header("Location: ./FILES/index.php");
}
}
}
function download_files($file_path, $start_time=null,$end_time=null ,$format)
{
$files = scandir($file_path, 1);
$suffix = ".$format";
$files_array = array();
foreach ($files as $index => $filename){
$pos = strpos($filename, $suffix);
if ($pos != false) {
if(strpos($filename,"_")){
$month_files = substr($filename,stripos($filename,"_")+1,8);
}else{
$month_files = substr($filename, 0, 8);
}
if($month_files >= $start_time && $month_files <= $end_time)
{
array_push($files_array, $filename);
}
}
}
if($format == "pdf")
{
download_pdf_files($file_path,$files_array,$start_time,$end_time);
}
else if($format == "html")
{
download_html_report_files($file_path,$files_array,$start_time,$end_time);
}
}
function download_pdf_files($file_path,$files_array,$start_time=null,$end_time=null)
{
if(is_dir($file_path))
{
$today = date("YmdHis");
$tmp_dir = "pdf_".$start_time."_".$end_time;
$cmd = "cd $file_path;";
$cmd .= "mkdir -p $tmp_dir;";
foreach($files_array as $index => $file)
{
$cmd .= "cp -a $file_path$file $tmp_dir;";
}
$cmd .= "tar cvzf $tmp_dir.tgz $tmp_dir;";
$cmd .= "rm -rf $tmp_dir;";
exec($cmd);
file_export($file_path.$tmp_dir.".tgz");
$cmd = "rm -rf $tmp_dir.tgz;";
exec($cmd);
}
else
{
file_export($file_path);
}
}
function download_html_report_files($file_path,$files_array,$start_time=null,$end_time=null)
{
if(is_dir($file_path))
{
$output_dir = "html_".$start_time."_".$end_time;
$files_dir = $file_path;
}
else
{
$names = split("\.",basename($file_path));
$output_dir =$names[0];
$files_dir = dirname($file_path);
}
$cmd = "mkdir -p $output_dir/HTML;";
$cmd .= "mkdir -p $output_dir/IMG;";
$cmd .= "cp ".$files_dir."/htmreport.css ".$output_dir."/HTML;";
foreach ($files_array as $index => $file)
{
$basename = split("\.", $file);
$cmd .= "cp ".$files_dir."/".$basename[0].".html ".$output_dir."/HTML;";
$image_files = get_html_image_files($basename[0]);
$cmd .= "cp ";
foreach ($image_files as $image_file)
{
$cmd .= "$image_file ";
}
$cmd .= " $output_dir/IMG/;";
}
$output_file = $output_dir.".tgz";
$cmd .= "cd $output_dir;cd ..; tar cvzf ".$output_file." ".$output_dir.";";
$cmd .= "rm -rf ".$output_dir.";";
exec($cmd);
file_export($output_file);
exec("rm -rf $output_file");
}
function get_html_image_files($basename)
{
$timeinfo = substr($basename,-14,-1);
$image_dir = $_SERVER[DOCUMENT_ROOT].'/report/IMG/';
$image_files = array();
$files = scandir($image_dir,1);
foreach ($files as $file)
{
$image_basename = substr($file,0,strlen($timeinfo));
if($image_basename == $timeinfo) $image_files[] = $image_dir.$file;
}
return $image_files;
}
function file_export($file)
{
//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/x-zip-compressed"; break;
case "rar": $ctype="application/x-rar"; break;
default: $ctype="application/force-download";
}
//Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
//Use the switch-generated Content-Type
header("Content-Type: $ctype");
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
# workaround for IE filename bug with multiple periods / multiple dots in filename
# that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
$iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
header("Content-Disposition: attachment; filename=\"$iefilename\"");
//header("Content-Range: $from-$to fsize"); 加上压缩包头信息不正确
//header("Content-Length: $content_size"); 加上压缩包头信息不正确
} else {
header("Content-Disposition: attachment; filename=\"$filename\"");
//header("Content-Range: $from-$to fsize"); 加上压缩包头信息不正确
//header("Content-Length: $content_size"); 加上压缩包头信息不正确
}
header("Accept-Ranges: bytes");
//header('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
$size=filesize($file);
//open the file
$fp=fopen("$file","rb");
//ek to start of missing part
fseek($fp,$range);
//start buffered download
while(!feof($fp)){
//reset time limit for big files
print(fread($fp,1024*8));
//flush(); 这个是多余的函数,加上会使压缩包下载不完整
//ob_flush(); 这个也是多余的函数,加上会使压缩包下载不完整
}
fclose($fp);
}
单色彩虹