Force download script not handling files with spaces properly


Posted on 16th Feb 2014 07:03 pm by admin

I have a regular old php force download script, uses this code:

Code: header("Cache-Control: ");
header("Pragma: ");
Did you know?Explore Trending and Topic pages for more stories like this.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="".$download.""");
header("Content-length:".(string)(filesize($path.$download)));
readfile($path.$download);
Where path is preset and $download is set by GET variables (don't worry, I've sanitized, I check the GET value against an array of acceptable filenames.) This works fine on my computer in both Internet Explorer and Firefox:

http://infectionist.com/music/

But I recently got the new Droid phone and found a bug. I cannot download files with spaces in the filename. I've tried everything I can think of and nothing is working. I even tried downloading a file with a space (failed), then renaming that file replacing the space with an underscore, then it worked. So I know that it's space handling that's the issue, I just can't figure out how to get around it!

The whole script is one that lists all the files in a directory that are of a certain extension (in this case mp3, wma, and wav). Each filename is put into an array, this array is used as the check against the GET variable's input. I then array_slice() the array to show 10 results per page. I foreach() the sliced array and print out information about the file, like filesize, filename, format, and a little icon. The filename is a link to download the file, printed like this:

<a href="?file=".urlencode($filename)."">

So spaces get replaced with a + sign in the URL, but get read as a space by the script. I think the problem lies with the readfile() line, I don't know why the Droid phone doesn't like it, but there HAS to be a way to fix this.

If someone, ANYONE, can help me, it will be very much appreciated.
No comments posted yet

Your Answer:

Login to answer
287 Like 37 Dislike
Previous forums Next forums
Other forums

Dynamic links in an include file
I am working on a small piece of code that counts records from a database and displays the result to

PHP form help required
Hi all! Apologies in advance for having to message the forum for help. I know you must get a lot of

Prevention Help
How can I use PHP or any other language (feel free to move this thread) to prevent users doing scree

Search function
I am looking for some guidance from the experts.

I am trying to create a search function. It

PHP switch function help
Hi, I am trying to create a search function for a directory website. On the search function on html

Download Image from URL and Upload it
Ok, I have been googling for the last 1/2 hour trying to figure this out, Most likely I'm just not s

issues verifying if user is logged in
I am having issues when a user logs in via asp Login control, the IsAuthenticated still seems to com

Check Digits and extracting digits
I think I can figure out the code for this but I am having a very hard time getting past the first s

IP Based Cron Jobs
I run cronjobs on my website. After one of my visitors got curious and tinkered around he realized h

Need help: how to catch acess of undefined class properties
Hello. I am learning OO with PHP and have hit a problem.
Some code runs as perfectly valid code,

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash