当前位置: 首页 > news >正文

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;}

http://www.lryc.cn/news/321092.html

相关文章:

  • 2024年企业经济管理与大数据国际会议(ICEEMBD 2024)
  • 数新网络助阵哈工大国家级项目,共绘数智化新篇章,打造大数据法务平台
  • React+umi+dva 项⽬实战-lesson6
  • Vue el-table 合并单元格
  • 面试算法-61-二叉树的右视图
  • 【鸿蒙HarmonyOS开发笔记】动画过渡效果之布局更新动画
  • 过路费的题解
  • 51单片机LED8*8点阵显示坤坤跳舞打篮球画面
  • C++_day6:2024/3/18
  • 汇编语言和IBM的关系
  • 堆(数据结构)
  • 医药工厂5G智能制造数字孪生可视化平台,推进医药企业数字化转型
  • C语言学习--八种排序算法
  • Infineon_TC264智能车代码初探及C语言深度学习(二)
  • 第十三届蓝桥杯(C/C++ 大学B组)
  • 数据结构从入门到精通——排序的概念及运用
  • react面试题总结
  • 5_springboot_shiro_jwt_多端认证鉴权_禁用Cookie
  • 条形码申请指南:外地人如何成功注册香港条形码
  • Covalent Network借助大规模的历史Web3数据集,推动人工智能发展
  • test测试类-变量学习
  • 【DL经典回顾】激活函数大汇总(二十七)(Bent Identity附代码和详细公式)
  • element-plus el-table表格默认选中某一行
  • Vue+SpringBoot打造民宿预定管理系统
  • 基于单片机的模糊PID炉温控制系统设计
  • 深入浅出落地应用分析:AI数字人「微软小冰」
  • 【早鸟优惠|高录用|EI稳定检索】2024年虚拟现实、图像和信号处理国际学术会议(ICVISP 2024)诚邀投稿/参会!
  • CPU设计实战—异常处理指令
  • Elasticsearch(13) match_phrase的使用
  • 通过路由器监控,优化网络效率