Skip to content
Advertisement

PHP file download not recognizing file type in Chrome and Edge

I’m trying to create a simple download page, which currently works in Firefox but not entirely in Chrome and Edge. The file to download is an mp3, located in a private directory on the server.

When I download the file in Firefox it works as intended. If I download it using Chrome or Edge the file still downloads, but it doesn’t recognize the file type and just saves it as an extensionless file.
If I add the .mp3 extension manually to the downloaded file it’s fine and I can play the track.

(A POST request is sent from index.php to download.php)
—————–
In download.php I set the following HTTP headers and read the file:

header("Cache-Control: private"); 
header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename="$title"");
header("Content-Length: ".filesize($location)); 

//Force download
readfile($location);
exit();

Changing the ‘Content-type’ to ‘audio/mpeg’ also doesn’t work.

Most articles I read about forcibly downloading a file using PHP are contradicting one another (especially about HTTP headers) and I can’t find an answer specifically to my situation.

I can also provide the full code if the problem doesn’t lie with the headers? Thank you.

Advertisement

Answer

can you try:

header("Content-Disposition: attachment; filename="$title".mp3");

to see if the title does not contain the extension

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement