zoukankan      html  css  js  c++  java
  • PHP文件下载出现乱码

    问题:

    一个用了很久的文件下载函数;

    <?php

    /**
     * 强制下载文件
     * @param string $filename 变量
     * @param string $name 变量
     * @return mixed
     */
    function download($filename,$name){
      if ((isset($filename))&&(file_exists($filename))){
         header("Content-length: ".filesize($filename));
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename="' . $name . '"');
         readfile("$filename");
      } else {
         $info="Looks like file does not exist!";
         return $info;
      }
    }

    ?>

    再次使用功能可以实现;

    但是下载下来的文件内容出现了乱码;

    --------------------------------------------------------------------------------

    解决办法:

    在下载之前使用两个函数处理一下:

    <?php

    /**
     * 强制下载文件
     * @param string $filename 变量
     * @param string $name 变量
     * @return mixed
     */
    function download($filename,$name){
      if ((isset($filename))&&(file_exists($filename))){
         header("Content-length: ".filesize($filename));
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename="' . $name . '"');
         ob_clean();  
         flush();
         readfile("$filename");
      } else {
         $info="Looks like file does not exist!";
         return $info;
      }
    }

    ?>

  • 相关阅读:
    Leetcode-Minimum Depth of Binary Tree
    Leetcode-Path Sum II
    Leetcode-Path Sum
    Leetcode-Flatten Binary Tree to Linked List
    Leetcode-Populating Next Right Pointer in Binary Tree II
    Leetcode-Pascal's Triangle II
    Leetcode-Pascal's Triangle
    Leetcode-Triangle
    第10月第20天 afnetwork like MKNetworkEngine http post
    第10月第13天 xcode ipa
  • 原文地址:https://www.cnblogs.com/eis13/p/5652496.html
Copyright © 2011-2022 走看看