php 页面中下载文件|图片

一、需求

页面中点击下载图片

二、实现

protected function pageLoad()
	{
	    $filePath = $_GET['file'];
        $host = $_SERVER['HTTP_HOST'];
        $file =  'http://'.$host.$filePath;
        $fileName = basename($filePath);
        $content = file_get_contents($file);
        ob_clean();
        $suffixArr = explode('.', $fileName);
        $suffix = strtolower($suffixArr[count($suffixArr)-1]);
        $suffixArr[count($suffixArr)-2] .= time();
        $fileName = implode('.', $suffixArr);
        $this->download($suffix, $content, $fileName);
	}

    private function download($extension,$content,$fileName='')
    {
        ob_clean();
        if($content)
        {
            switch ($extension)
            {
                case 'jpg':
                case 'jpeg':
                case 'jpe':
                case 'jfif':
                    header('Content-Type: image/jpeg');
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'png':
                    header('Content-Type: image/png');
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'bmp':
                    header('Content-Type: image/bmp');
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'gif':
                    header('Content-Type: image/gif');
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'tif':
                case 'tiff':
                    header('Content-Type: image/tiff');
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'swf':
                    header("Content-type: application/x-shockwave-flash");
                    if ($fileName)
                    {
                        header('Content-Disposition: attachment; filename='.$fileName);
                    }
                    break;
                case 'doc':
                case 'docx':
                case 'ppt':
                case 'pptx':
                    $fileName = !empty($fileName)? $fileName:'';
                    header("Content-type: application/octet-stream");
                    header("Accept-Ranges: bytes");
                    header('Content-Disposition: attachment; filename='.$fileName);
                    header("Pragma:no-cache");
                    header("Expires:0");
                    break;
                case 'pdf':
                    $fileName = !empty($fileName)? $fileName:'';
                    header("Content-type: application/octet-stream");
                    header("Accept-Ranges: bytes");
                    header('Content-Disposition: attachment; filename='.$fileName);
                    header("Pragma:no-cache");
                    header("Expires:0");
                    break;
                case 'txt':
                    if ($fileName)
                    {
                        header("Content-type: application/octet-stream");
                        header("Accept-Ranges: bytes");
                        header('Content-Disposition: attachment; filename='.$fileName);
                        header("Pragma:no-cache");
                        header("Expires:0");
                    }
                    break;
                case 'html':
                    header("Cache-Control: no-cache, must-revalidate");
                    header("Pragma: no-cache");
                    header("Content-Type: application/force-download");
                    header("Content-type: text/html");
                    header("Content-Disposition: attachment; filename=".$fileName);
                    header("Content-Description: PHP5 Generated Data");
                    break;
                case 'xls':
                case 'xlsx':
                    $fileName = !empty($fileName)? $fileName:'';
                    header("Content-type:application/octet-stream");
                    header("Accept-Ranges:bytes");
                    header("Content-type:application/vnd.ms-excel");
                    header('Content-Disposition: attachment;filename='.$fileName);
                    header("Pragma: no-cache");
                    header("Expires: 0");
                    break;
            }
        }
        echo $content;
        exit;
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值