代码之家  ›  专栏  ›  技术社区  ›  Stuart

下载不在IE中工作的文件的PHP脚本

  •  11
  • Stuart  · 技术社区  · 17 年前

    我有一个脚本,它从$_GET['key']中获取一个键,在数据库中查找位置,并使用readfile和一些头来提供下载供使用。这在Firefox中有效,但在IE8中无效,无法在其他IE上测试。我在IE中遇到以下错误:“Internet Explorer无法从www.example.com下载download.php”。好像它正在尝试下载PHP脚本。

    
    $the_query = "SELECT * FROM `files` WHERE `user_id`=" . $_SESSION['user_id'] . " AND `key`='" . $key . "'";
    
    $result = mysql_query($the_query);
    $row = mysql_fetch_array($result);
    
    $file = '/var/www/vhosts/www.example.com/httpsdocs/uploads/' . $row['id'] . '/' . $row['file'];
    
    header("Content-type: application/octet-stream");
    header("Content-length: ".filesize($file));
    header('Content-Description: File Transfer');
    header("Cache-control: private");
    header('Content-Disposition: attachment; filename=' . rawurlencode(basename($file)));
    readfile($file);
    
    5 回复  |  直到 17 年前
        1
  •  28
  •   Jamie Taylor    12 年前

    为了解决错误:“Internet Explorer无法从www.example.com下载download.php”,

    header("Pragma: ");

    header("Cache-Control: ");

    该代码将从导致下载问题的标头中删除缓存控件。

    上述代码应添加到文件的顶部。

    这对我们来说很好。

        2
  •  9
  •   Stuart    17 年前

    http://us3.php.net/manual/en/function.readfile.php

    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
    
    
        3
  •  3
  •   Tyler Carter    17 年前

    替换此项:
    header("Content-type: application/octet-stream");

    header("Content-Type: application/force-download");

    post ,IE通常不会侦听您的标题,而是自行查找您发送的内容。

        4
  •  1
  •   ABDULRAHMAN ALKHAMEES    5 年前

    将contentdispositionheader设置为“attachment”,就像(在PHP中)那样:这里是编写附件脚本的地方。

    header('Content-Disposition: attachment');
    

    并在.htaccess中添加以下内容,并添加您想要下载的扩展名,而不仅仅是txt

    <FilesMatch "\.(txt|pdf|csv|xls|xlsx|xlam|xlsb|xlsm|msg|doc|docx|mpg|jpg|png)">
       Header set Content-Disposition attachment
    </FilesMatch>
    
        6
  •  0
  •   John Doe    10 年前

    永远不要取代这个: 标题(“内容类型:应用程序/八位字节流”);

    为此:

    我在一次测试中尝试使用“application/zip”,因为我在技术上是在处理一个zip文件,但是IE6.0破坏了下载!不过,其他一切都表现正常。但是,是的,必须切换回“应用程序/八位字节流”,因此任何试图检测文件扩展名并切换到特定于扩展名的其他内容类型的代码都是有风险的!最好对所有二进制文件使用“应用程序/八位字节流”!

    推荐文章