首页 > 编程语言 >PHP别名下载文件(TP5框架)

PHP别名下载文件(TP5框架)

时间:2022-08-27 10:23:46浏览次数:63  
标签:url 别名 Content header TP5 file fileinfo PHP id

    public function index() {
        $id = (int)input('id');

        if(empty($id) || !is_numeric($id)){
            $this->error("参数错误");
        }

        $fileinfo = db('files')->where('id',$id)->find();
        if(!$fileinfo){
            $this->error("文件不存在!");
        }
        $file_url = './uploads/Download/'.$fileinfo['savepath'].$fileinfo['savename'];
        $out_filename = $fileinfo['name'];
        if(!file_exists($file_url)) {
            $this->error("文件不存在!");
        }else{
            header('Accept-Ranges: bytes');
            header('Accept-Length: ' . filesize( $file_url ));
            header('Content-Transfer-Encoding: binary');
            header('Content-type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $out_filename);
            header('Content-Type: application/octet-stream; name=' . $out_filename);
            if(is_file($file_url) && is_readable($file_url)){
                $file = fopen($file_url, "r");
                echo fread($file, filesize($file_url));
                fclose($file);
            }
        }
    }

 

标签:url,别名,Content,header,TP5,file,fileinfo,PHP,id
From: https://www.cnblogs.com/fengluoye/p/16629898.html

相关文章