Jump to content

Recommended Posts

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

 

header("Cache-Control: ");
  header("Pragma: ");
  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.

urldecode() does the opposite to urlencode() restoring the original string. however you should never have filenames or directories containing spaces, never, never, never! especially on the web. you should get used to this on any computer, although windows allows you to create files with spaces in their names you should consider it bad practice.

 

If files are uploaded to your website by a user, your script should modify its filename to make it safe.

definitely adhere to what neil.johnson said about handling the issue at time of upload (do not allow for spaces in file names).

 

in the meantime, check out my post a little while back regarding this issue, and make any changes to suit your needs.  might help you in your case, i don't know.  but it looked like a similar issue.

 

http://www.phpfreaks.com/forums/index.php/topic,278502.msg1318858.html#msg1318858

Well I suppose that solves that.  I' sure I could whip up a quick script to go through and rename all files with spaces to replace spaces with, say, an underscore.  That would fix it.  I wanted to avoid that because I have direct links to some of those files on my forum, but I'm sure I can do a quick database search for any links at the base url and do a replace.  Thanks for the help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.