Are spaces in the title breaking the download?

So, I have this code that will allow the 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 there is a space in the song, like sinsita happy 1 SONIFI.mp3 , the user will only upload a text file named sinsita ... any ideas for fixing this behavior

+7
source share
1 answer

You must specify the file name in the content location:

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

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

 header('Content-Disposition: attachment; filename="' . str_replace('"', '\\"', ($mask ? $mask : basename($file))) . '"'); 
+11
source

All Articles