zoukankan      html  css  js  c++  java
  • php 实现文件下载,兼容IE、Firefox、Chrome等浏览器

    一、下载任意文件:

    Header ( "Content-type: application/octet-stream" );
    $ua = $_SERVER ["HTTP_USER_AGENT"];
    $file = '/var/www/tmp.txt';
    $filename = basename ( $file );
    $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 );

    二、PHPExcel导出excel文件下载:

         ...
    $objWriter
    = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); header('Content-Type: application/vnd.ms-excel'); $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('Cache-Control: max-age=0'); $objWriter->save('php://output');
  • 相关阅读:
    [置顶] 怎么对待重复的代码
    AIX和Linux中wtmp的不同处理方式
    Visio 下载,及密钥
    全局变量和局部变量
    UNIX网络编程--IPV4 IPV6 ICMPV4 ICMPV6
    Android XML文档解析(一)——SAX解析
    rnqoj-30- [stupid]愚蠢的矿工-树形DP
    linux 文件内容的复制
    主流视音频平台参数
    FTP原理
  • 原文地址:https://www.cnblogs.com/h07061108/p/download_file.html
Copyright © 2011-2022 走看看