php使用HttpFoundation进行下载文件

  1. php使用HttpFoundation进行下载文件–php处理下载

1、使用composer安装HttpFoundation组件

composer require symfony/http-foundation -v 3.4.*

2、目录结构
在这里插入图片描述
3、FileDownload.php对httpfoundation组件下载功能封装(支持中文文件名)

<?php
namespace download;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Request;

class FileDownload extends BinaryFileResponse{
   
	/**
	 * 构造函数
	 * @param  string  $file               文件路径
	 * @param  integer $status             响应状态
	 * @param  array   $headers            响应头信息
	 * @param  boolean $public             Cache-Control
	 * @param  string  $contentDisposition Content-Disposition
	 * @param  boolean $autoEtag           Etag
	 * @param  boolean $autoLastModified   LastModified
	 * @return BinaryFileResponse
	 */
	public function __construct($file, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
	{
   
        $file = $this->encoding($file, 'GBK', 'UTF-8');
        parent::__construct($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
	}

    /**
     * 使用php进行文件下载
     * @param  string $filename 显示文件名(不包含后缀)
     */
    public function download($showname = '')
    {
   
        $showname = $this->getShowName($showname);
    	$this->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $showname);

        $request = Request::createFromGlobals();
    	$this->prepare($request)->send();
    }

    /**
     * 使用X-SendFile进行文件下载
     * @param  string $filename 显示文件名(不包含后缀)
     */
    public function xDownload($showname, $Sendfile = 'X-Sendfile')
    {
   
        $showname = $this->getShowName($showname);

        $this->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $showname);

        $request = Request::createFromGlobals();
        $request->headers->set('X-Sendfile-Type', $Sendfile);
        //设置使用X-SendFile下载文件
        BinaryFileResponse::trustXSendfileTypeHeader();
        
        $this->prepare($request
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值