zoukankan      html  css  js  c++  java
  • php中使用header在下载时乱码问题解决

    这段时间在做一个项目,有一个下载模块。

    通过一个php文件为中介,实现下载权限控制,隐藏真实地址。

    header('Content-Disposition: attachment; filename="' . $showname . '.'.$filetype.'"');

    在这里定义了,附件,文件名。

    但是下载的生活在FF,webkit浏览器下均正常,但是在IE下面不正常。

    在网上找了很久。后来发现源码里面已经带了,但是自己没有发现。故特此记录下。

    在这里学习到一个新的函数rawurlencode ,至于这个跟urlencode有什么区别还不太清楚。

    知道的朋友不妨能告诉我一下。

    View Code
    $module = $module ? $module : MODULE_NAME;
    $id = $id ? $id : intval($_REQUEST['id']);
    $this->dao = M($module);
    $filepath = $this->dao->where("id=" . $id)->getField('file');
    $this->dao->where("id=" . $id)->setInc('downs');
    
    if (strpos($filepath, ':/')) {
    header("Location: $filepath");
    } else {
    if (!$filename)
    $filename = basename($filepath);
    $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($useragent, 'msie ') !== false)
    $filename = rawurlencode($filename);
    $filetype = strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
    $realpath = $_SERVER['DOCUMENT_ROOT'].$filepath;
    $filesize = sprintf("%u", filesize($realpath));
    $showname = $this->dao->where("id=" . $id)->getField('title');
    if (strpos($useragent, 'msie ') !== false)
    $showname = urlencode ($showname);
    if (ob_get_length() !== false)
    @ob_end_clean();
    header('Pragma: public');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: pre-check=0, post-check=0, max-age=0');
    header('Content-Transfer-Encoding: binary');
    header('Content-Encoding: utf-8');
    header('Content-type: ' . $filetype);
    header('Content-Disposition: attachment; filename="' . $showname . '.'.$filetype.'"');
    header('Content-length: ' . $filesize);
    readfile($realpath);
  • 相关阅读:
    fabric.js 学习
    前端基础回顾
    ubuntu16 mysql 远程连接
    Django rest_framework API 随笔
    django 学习笔记
    vue 返回上一页在原来的位置
    Django mysql 改用pymysql 驱动
    Material-UI
    设置placeholder的样式
    Cookie、session和localStorage、以及sessionStorage之间的区别
  • 原文地址:https://www.cnblogs.com/MichaelZhangX/p/2686039.html
Copyright © 2011-2022 走看看