Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces in the name is breaking the download?

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

like image 746
Matt Elhotiby Avatar asked Jun 29 '11 02:06

Matt Elhotiby


1 Answers

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))) . '"');
like image 85
vcsjones Avatar answered Sep 28 '22 12:09

vcsjones