用PHP实现PDF文件内容的下载(非URL方式)

以下是下载PDF文件的PHP的实现函数:

<?php

function downloadFile( $sFile  , $sTopic)

    // Must be fresh start 

    if( headers_sent() ) 

     die('Headers Sent'); 

  

    // Required for some browsers 

    if(ini_get('zlib.output_compression')) 

         ini_set('zlib.output_compression', 'Off'); 

  

    if( file_exists($sFile) ){ 

         $fsize = filesize($sFile);   //文件大小

         header("Pragma: public"); 

         header("Expires: 0"); 

         header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

         header("Cache-Control: private",false); 

         header("Content-Type: application/pdf");   //PDF类似

         header("Content-Disposition: attachment; filename="".basename($sTopic)."";" );   //下载时提示的名称

         header("Content-Transfer-Encoding: binary"); 

         header("Content-Length: ".$fsize); 

         ob_clean(); 

         flush(); 

         readfile( $sFile ); 

    } else 

    {

         die('File Not Found'); 

    }

  

  } 

?>

调用上述函数的代码为:

<?php

 downloadFile( "http://www.xiaojin.net/downs/1003001.pdf"  , "示例.pdf");

?>


 上一个     下一个