TP框架實現文件的下載(主要解決文件存在中文文件名的問題)


namespace Home\Controller;
use Think\Controller;
use Org\Net\Http;
class IndexController extends Controller {

    public function index(){
           $file_id = I( 'get.file_id' , 0 );
           if( $file_id == 0 ){
                  $this -> error( '文件不存在' );
           }
           $db_file = M( 'file' );
           $condition [ 'file_id' ] = $file_id;
           $file_result = $db_file -> where( $condition ) -> find( );
           if( empty( $file_result ) || ! file_exists( $file_result[ 'file_path' ] ) ){
                  $this -> error( '文件不存在' );
           }
           $showname = $file_result[ 'show_name' ];
           $ext_arr = explode( '.' , $file_path );
           $showname .= '.'.$ext_arr[ count( $ext_arr ) -1 ];
           $encoded_filename = urlencode( $showname );
           $encoded_filename = str_replace( "+" , "%20" , $encoded_filename );
           Http::download( $file_result[ 'file_path' ] , $showname );
    }

}

 


免責聲明!

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



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