You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firefox下载时retrieve.php替代Content-Disposition指定文件名问题求助

Fixing Firefox Download Issue with Your retrieve.php Zip Script

Hey there! I totally get how frustrating it is when a script works everywhere except one browser—Firefox has some specific quirks with HTTP headers that often trip things up. Let's walk through the most likely fixes for your problem where Firefox is using retrieve.php as the download filename instead of your specified zip name.

Common Culprits & Solutions

1. Update Your Content-Disposition Header for Firefox Compatibility

Firefox is stricter about following RFC standards for the Content-Disposition header, especially when it comes to filenames. A lot of scripts only use the basic filename parameter, but you need to add the filename* variant to ensure Firefox picks up the correct name (this also helps with non-ASCII filenames).

Here's how to adjust your header code:

$zipFileName = "your-custom-archive.zip"; // Replace with your actual filename

// Encode the filename properly for RFC 5987 compliance
$encodedFileName = rawurlencode($zipFileName);

// Set headers with both filename formats for broad compatibility
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"$zipFileName\"; filename*=UTF-8''$encodedFileName");

2. Clean Up Unwanted Output Before Sending Headers

Firefox will ignore your filename header if there's any extra output (like whitespace, error messages, or BOM characters) before the headers are sent. Even a single space outside your PHP tags can break things.

Add these lines right before you set your headers to clear the output buffer:

// Clear any existing output from buffers
ob_clean();
flush();

// Now set your headers and output the zip

Also double-check all included/required files in your script—make sure none of them have extra spaces or newlines after their closing ?> tag (you can even omit the closing tag entirely to avoid this issue).

3. Add Cache-Control Headers to Avoid Old Response Caching

Sometimes Firefox caches an old version of the response where the filename was incorrect. Adding these headers ensures the browser gets a fresh response every time:

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

4. Verify Your Zip Generation Logic Isn't Outputting Errors

If your zip creation code throws warnings or errors (even silent ones), those can sneak into the response stream and corrupt the headers. Make sure you're suppressing errors during zip generation or handling them properly, and confirm the zip file is being created successfully before sending it to the browser.

Full Example Snippet

Putting it all together, your download section might look like this:

// Assume $zipFilePath is the path to your generated zip file

// Clean up any stray output
ob_clean();
flush();

// Set proper headers
header('Content-Type: application/zip');
$zipFileName = "my-download-archive.zip";
$encodedFileName = rawurlencode($zipFileName);
header("Content-Disposition: attachment; filename=\"$zipFileName\"; filename*=UTF-8''$encodedFileName");
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

// Output the zip stream
readfile($zipFilePath);
exit; // Ensure no further code runs after output

Give these changes a try—9 times out of 10, it's either the header format or stray output causing Firefox to misbehave.

内容的提问来源于stack exchange,提问作者SimonC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:39:50