<?php
// 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf');// 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');// 这是 original.pdf 的源文件
readfile('original.pdf');
?>

解决方案 »

  1.   

    zhangli() :谢谢!我在网上找到的资料也是类似这样说的。有个问题,我怎么知道要下载的文件是什么类型的?
    就是说下面要根据文件类型不同而变的:
    header('Content-type: application/pdf');
      

  2.   

    你可以判断一下后缀名,写个switch...
      

  3.   

    http://www.bumt.cn/blog/article.php?id=16&sortid=3
      

  4.   


    header('Content-type: application/pdf');
    改成,都按照二进制文件处理
    header("Content-type: application/octet-stream");
      

  5.   

    谢谢各位,我的文件可以下载了,但是。下载后基本上都打不开,试过jpg、rar、doc,好像下载后文件大小都不同了。我的程序如下,请指教:<?$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(); ?>
      

  6.   

    粘错了,以下才是:
    <?$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(); ?>
      

  7.   

    // imgfile  是参数  代表文件的路径 + 文件名<?php
    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);
    }
    ?>
      

  8.   

    function downFile(downfile)
        {
    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