gpt4 book ai didi

php - 使用 PHP 发送文件时在 Internet Explorer 中维护自定义文件扩展名?

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:29 24 4
gpt4 key购买 nike

我有一个服务器端 zip 存档,我想将其作为下载传递。我正在使用自定义扩展将这些特定存档与某些客户端软件(例如 CustomArchive.bwz)相关联。

Chrome 和 FireFox 完美地处理了这个自定义扩展,但 Internet Explorer 一直在文件名末尾添加“.zip”(例如 CustomArchive.bwz.zip)。

这是我的下载文件方法:

function downloadFile($file)
{
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;
}

举个例子,我这​​样调用它:

downloadFile( "CustomArchive.bwz" );

其中 CustomArchive.bwz 只是一个带有自定义扩展名的 zip 文件。

尝试从我的 downloadFile 方法中删除各种位后,我能够将其缩小到调用 ob_clean()。不幸的是,删除该调用会导致下载损坏。

是否有解决方法可以强制 IE 使用 .bwz 扩展名而不添加 .zip?

我的服务器在 IIS 7.5 上运行 PHP 5.3.5。

最佳答案

来自 marcelebrate:使用 application/force-download 而不是 application/octet-stream 是阻止 IE 假设我的文件是 zip 存档的原因。

function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';

header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
header('Content-Length: ' . filesize($file));

//from Christian Sciberras: There may (often) be multiple levels of output buffering.
while(ob_get_level()) ob_end_clean();
ob_implicit_flush(true);

readfile($file_name); // push it out
exit();
}

关于php - 使用 PHP 发送文件时在 Internet Explorer 中维护自定义文件扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com