Filenames with spaces are break the download in Mozilla Firefox


When downloading certain files, you may find that the filename is truncated up to the first space. Thus, a link to download the file “My music.mp3” produces a save dialog containing “My” as the filename. This is a case of the website incorrectly sending the filename, and the browser coping as best it can.

if the file name contains a quote or spaces; then you have to escape that quote or spaces.

So you can use following code for download mp3 file for the solve this issue.

header(‘Content-Disposition: attachment; filename=”‘ . str_replace(‘”‘, ‘\\”‘, $fileName) . ‘”‘);

downlaod.php file:

 <?php

 // $audioFile = "Maid with the Flaxen Hair.mp3";
 $fileName = $_GET['id'];
 // Fetch the file info.
 $filePath = '../uploads/music/' . $fileName;
 
 if(file_exists($filePath)) {
 $fileName = basename($filePath);
 $fileSize = filesize($filePath);

 // Output headers.
 header("Cache-Control: private");
 header("Content-Type: application/force-download");
 header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
 header("Content-Length: ".$fileSize);
// header("Content-Disposition: attachment; filename=".$fileName);
 header('Content-Disposition: attachment; filename="' . str_replace('"', '\\"', $fileName) . '"');

 // Output file.
 readfile ($filePath); 
 exit();
 }
 else {
 die('The provided file path is not valid.');
 }

?>

html file:

 <a href="http://localhost/musiclib/include/download.php?id=my music.mp3"> Download</a>
Advertisements

One comment

  1. I read a lot of interesting posts here. Probably you spend a lot
    of time writing, i know how to save you a lot of work, there is an online tool that creates unique, google friendly posts in minutes,
    just search in google – laranita free content

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s