php實現當前頁面點擊下載文件的實例


php控制器中代碼

public function downFile($path ''){

     if (! $path ) header( "Location: /" );
     download( $path );
   }
download文件下載函數代碼
function download($file_url,$new_name=''){
     if (!isset( $file_url )||trim( $file_url )== '' ){
       echo  '500' ;
     }
     if (! file_exists ( $file_url )){  //檢查文件是否存在
       echo  '404' ;
     }
     $file_name = basename ( $file_url );
     $file_type = explode ( '.' , $file_url );
     $file_type = $file_type [ count ( $file_type )-1];
     $file_name =trim( $new_name == '' )? $file_name :urlencode( $new_name );
     $file_type = fopen ( $file_url , 'r' );  //打開文件
     //輸入文件標簽
     header( "Content-type: application/octet-stream" );
     header( "Accept-Ranges: bytes" );
     header( "Accept-Length: " . filesize ( $file_url ));
     header( "Content-Disposition: attachment; filename=" . $file_name );
     //輸出文件內容
     echo  fread ( $file_type , filesize ( $file_url ));
     fclose( $file_type );
}
html代碼
<iframe id="iframe" src="" style="display: none;"></iframe>
$( function (){
       $( '.downLoad' ).click( function (){
         var  path = $(this).attr( 'path' );
         $( '#iframe' ).attr( 'src' , "php文件路徑?path=" +path);
       })
     })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM