TP框架下载功能

复制代码
namespace Home\Controller;
use Think\Controller;
use Org\Net\Http;
class IndexController extends Controller {

    public function download(){
      //获取文件的唯一id(这里可以将展示页面的对应表的id保存在session里,然后使用download方法获取该id) $file_id = session('id'); if( $file_id == 0 ){ $this -> error( '文件不存在' ); } $db_file = M( '表名' );
       //匿名组合数据 $condition [ 'file_id' ] = $file_id;
       //通过上面条件查找数据表文件路径 $file_result = $db_file -> where( $condition )->field('file_path') -> 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 ); } }

转载于:https://www.cnblogs.com/mo-yang/p/6395749.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值