Ok so i have this code that will allow a user to download a song

$file = DIR_DOWNLOAD . $download_info->row['filename'];
$mask = basename($download_info->row['mask']);
$mime = 'application/octet-stream';
$encoding = 'binary';

if (!headers_sent()) {
if (file_exists($file)) {
    header('Pragma: public');
    header('Expires: 0');
    header('Content-Description: File Transfer');
    header('Content-Type: ' . $mime);
    header('Content-Transfer-Encoding: ' . $encoding);
    header('Content-Disposition: attachment; filename=' . ($mask ? $mask : basename($file)));
    header('Content-Length: ' . filesize($file));
    $file = readfile($file, 'rb');

The problem is that if the song has a space in it like sinsita happy 1 SONIFI.mp3 the user will only download a text file named sinsita ...any ideas how to fix this behavior

Accepted Answer

You have to quote the file name in the content disposition:

header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');

Edit: Now that also means that if the file name contains a quote; then you have to escape that quote. So really your code looks like this now:

header('Content-Disposition: attachment; filename="' . str_replace('"', '\\"', ($mask ? $mask : basename($file))) . '"');
Written by vcsjones
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki