zoukankan      html  css  js  c++  java
  • php 文件下载代码

    1.对于一般

    $file = "/tmp/dummy.tar.gz";
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header("Content-Length: ". filesize($file));
    readfile($file);

    2.对于下载的文件的名称是中文的,用户下载后肯能出现乱码

    $file = "/tmp/中文名.tar.gz";
    
    $filename = basename($file);
    
    header("Content-type: application/octet-stream");
    
    //处理中文文件名
    $ua = $_SERVER["HTTP_USER_AGENT"];
    $encoded_filename = rawurlencode($filename);
    if (preg_match("/MSIE/", $ua)) {
     header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
     header("Content-Disposition: attachment; filename*="utf8''" . $filename . '"');
    } else {
     header('Content-Disposition: attachment; filename="' . $filename . '"');
    }
    
    header("Content-Length: ". filesize($file));
    readfile($file);

    3.我们可以不用readfile,换用其他的也行。

    $file = "/tmp/中文名.tar.gz";
    
    $filename = basename($file);
    
    header("Content-type: application/octet-stream");
    
    //处理中文文件名
    $ua = $_SERVER["HTTP_USER_AGENT"];
    $encoded_filename = rawurlencode($filename);
    if (preg_match("/MSIE/", $ua)) {
     header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
     header("Content-Disposition: attachment; filename*="utf8''" . $filename . '"');
    } else {
     header('Content-Disposition: attachment; filename="' . $filename . '"');
    }
    
    //让Xsendfile发送文件
    header("X-Sendfile: $file");
  • 相关阅读:
    成为数据科学家并不难
    大数据分析的众包平台—Kaggle
    如何写一份好的数据分析报告
    配置java环境时,java的path地址放在其他地址的前面还是后面?
    js:随记
    JS:JSP Servlet
    [swustoj 1088] 德州扑克
    [swustoj 1092] 二分查找的最大次数
    [swustoj 371] 回文数
    [swustoj 1097] 2014
  • 原文地址:https://www.cnblogs.com/wuheng1991/p/6527110.html
Copyright © 2011-2022 走看看