Page 1 of 1

Firefox Truncating Download Filenames

Posted: Sun Nov 27, 2005 8:16 am
by BZorch
As recommended to me in a post a while back

viewtopic.php?t=37539&highlight=

I am trying to download photo outside of the www folder using the following code. The script queries the database using the ID of the photo to pull the full file address to the file and the file name only (since it is already in the database and base() was truncating my file name). First, I use the script to insert the selected photo as a SRC element with a link to download a larger file. The SRC element works great in IE and Firefox, but the link for the download only works in IE. In Firefox the name gets truncated where there is a space in the file name. Firefox ask me to save or open the file, but the file name is truncated and when it saves the files it has nothing in it. Though files names that do not have spaces work fine.

Is this a bug in Firefox or is there something wrong in my script that IE is compensating for? Do have to get rid of all spaces in my file names (I fear this is what I may have to do)? Any suggestions? Thank you.

Code: Select all

<?php
if ($num =1) { // If query ran OK, display photo 
$val= ($row[0]); 
//update ULR to correct location on server
$val = str_replace("\\","/",$val);
$val = str_replace(".JPG",".jpg",$val);
$val = "../image_database/Medium Resolution/".str_replace("C:/local source/","",$val);
	
$SRC_FILE = $val;//address for photo to download
$download_size = filesize($SRC_FILE);//gets the file size of photo
$filename = ($row[1]); //gets only filename (from database instead of using base())
//download the photo 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-Type: image/jpg");
header("Content-Disposition: attachment; filename=$filename");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
@readfile($SRC_FILE);
			
mysql_free_result ($result); // Free up the resources.
				
mysql_close(); // Close the database connection.			
?>

Re: Firefox Truncating Download Filenames

Posted: Sun Nov 27, 2005 12:10 pm
by Roja
BZorch wrote: Is this a bug in Firefox or is there something wrong in my script that IE is compensating for? Do have to get rid of all spaces in my file names (I fear this is what I may have to do)? Any suggestions? Thank you.
Try doing a str_replace on space characters and converting them to %20. Urls cannot contain spaces, while %20 (the urlencoded version of space) is legal.

Posted: Sun Nov 27, 2005 2:52 pm
by BZorch
No luck. I tested to make sure that the spaces were replaced with %20 by printing the variable and it is replacing it correctly, but Firefox still truncates and IE shows it as an empty space. Thanks.

Posted: Sun Nov 27, 2005 2:53 pm
by Roja
BZorch wrote:No luck. I tested to make sure that the spaces were replaced with %20 by printing the variable and it is replacing it correctly, but Firefox still truncates and IE shows it as an empty space. Thanks.
Do you have a link where I can test and see what it does?

Posted: Mon Nov 28, 2005 6:55 am
by BZorch
Since the site is underdevelopment I am reluctant to give out the links to a particular page. Because of this you may not be able to help.

I was surprised that no one else is having this problem. Of course, everyone may have known to not include spaces in their filenames. I just may have to rename 7,000+ files.

The following is a little more description of what happens in Firefox.

Basically I have a link that says "Download Large File"

When selected the download dialogue comes up asking to open or save the file. The file name it shows in Firefox includes all letters in the filename up to the first white space. When I click to download, it downloads a file with nothing in it named the truncated file name.

Example1: 34th Street-001.jpg Becomes(Firefox): 34th Becomes(IE and Opera):34th Street-001.jpg

Example2: Misc-001.jpg Becomes(Firefox) Misc-001.jpg Becomes(IE and Opera):Misc-001.jpg

Posted: Mon Nov 28, 2005 6:58 am
by Roja
BZorch wrote:I was surprised that no one else is having this problem. Of course, everyone may have known to not include spaces in their filenames. I just may have to rename 7,000+ files.
Thats why I want to see some example code that produces the effect.

I have plenty of filenames with spaces in them that Firefox links correctly - as long as they are converted to %20's first.

Since Firefox is "working for me", and not for you, I can only think there is something wrong in the implementation, and not in the concept.

Posted: Mon Nov 28, 2005 7:15 am
by redmonkey
Have you tried quoting the filename? i.e....

Code: Select all

header("Content-Disposition: attachment; filename=\"{$file}\"");
.. I've had similar issues (can't remember which browser) but for me at least, quoting the filename within the header solved the problem.

Posted: Tue Nov 29, 2005 8:28 pm
by BZorch
I finally found some time to try your suggestions.

Adding the quotes worked!

Thanks to you both for your help.

BTW I can not find a good resource that explains all of the header information used. Does anyone know of a good tutorial for using headers? The PHP site is not very descriptive. As to what each element is or all of the choices. I would just like to learn a bit more because they seem like they can be very useful.

Thanks again for the help.