<?php
// 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf');// 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');// 这是 original.pdf 的源文件
readfile('original.pdf');
?>
// 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf');// 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');// 这是 original.pdf 的源文件
readfile('original.pdf');
?>
就是说下面要根据文件类型不同而变的:
header('Content-type: application/pdf');
header('Content-type: application/pdf');
改成,都按照二进制文件处理
header("Content-type: application/octet-stream");
$ext = substr($filepath,-3 );
$fp=fopen($filepath,"r");
$fileContent=fread($fp,filesize($filepath));
fclose($fp);switch($ext){
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "zip": $ctype="application/rar"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpg": $ctype="image/jpg"; break;
default:
echo "<html><body>您不可以下载本文件!</body></html>";
exit;
} header ("Content-type:$ctype");
header ("Content-Length".filesize($filepath));
header ("Content-Disposition: attachment; filename:".$filepath);
echo ($fileContent);
flush(); ?>
<?$filepath=$_GET[filename];
$ext = substr($filepath,-3 );
$fp=fopen($filepath,"r");
$fileContent=fread($fp,filesize($filepath));
fclose($fp);switch($ext){
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "zip": $ctype="application/rar"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpg": $ctype="image/jpg"; break;
default:
echo "<html><body>您不可以下载本文件!</body></html>";
exit;
} header ("Content-type:$ctype");
header ("Content-Length:".filesize($filepath));
header ("Content-Disposition: attachment; filename=".$filepath);
echo ($fileContent);
flush(); ?>
download_file($_GET["imgfile"]); function download_file($path_file){
ini_set('max_execution_time', 600);
if (!file_exists($path_file)) {
die("Error: File(".$path_file.") does not exist");
}
if (!($fp = fopen($path_file, "r"))) {
die("Error: Cannot open the file(".$path_file.")");
}
fclose($fp);
if (($content_length = filesize($path_file)) == 0) {
die("Error: File size is 0.(".$path_file.")");
}
header("Content-Disposition: inline; filename=\"".basename($path_file)."\"");
header("Content-Length: ".$content_length);
header("Content-Type: application/file");
if (!readfile($path_file)) {
die("Cannot read the file(".$path_file.")");
}
ini_set('max_execution_time', 30);
}
?>
{
ob_start(); if (!empty($downfile)) {
if (!@file_exists($downfile)):
echo '<script>alert(\'你要下载的文件不存在!\')</script>';
else:
$filename = basename($downfile);
$filename_info = explode('.', $filename);
$fileext = $filename_info[count($filename_info)-1];
header('Content-type: application/x-'.$fileext);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Description: PHP Generated Data');
header('Content-Length: '.filesize($downfile));//print_r(readfile($downfile));
@readfile($downfile);
exit;
endif;
}else
{
echo '<script>alert(\'你要下载的文件名未填写!\')</script>';
}
}//End Function