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);
  • 相关阅读:
    WebDriverAgent入门篇-安装和使用
    5分钟了解TypeScript
    “软到不行”的WWDC2018
    IntelliJ idea 撤回(已经commit未push的)操作
    【java并发核心一】Semaphore 的使用思路
    Spring Boot 如何干掉 if else?
    到底什么是重入锁,拜托,一次搞清楚!
    mysql 递归查找菜单节点的所有子节点
    sql语句递归查询(start with)
    js实现对上传图片的路径转成base64编码,并且对图片进行压缩,实现预览功能1
  • 原文地址:https://www.cnblogs.com/MichaelZhangX/p/2686039.html
Copyright © 2011-2022 走看看