Forum Moderators: coopster

Message Too Old, No Replies

Annoying Header/File Name Problem

Is This a PHP Issue, Browser Issue or HTTP Issue?

         

cmarshall

1:38 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a file downloader, and I use a header to create a filename. Thusly:

header("Content-Disposition: filename=".$row['_filename']);

in which "$row['_filename']" is extracted from a MySQL select, and contains the file's original name.

The problem occurs when the filename has more than one period in it, to wit:

Spec_Version1.3Delta.doc

The saved file is saved as "Spec_Version1.3Delta", and the ".doc" is truncated. If I make it "

Spec_Version1_3Delta.doc
," it saves fine.

I am using Firefox 2 on Mac. I'll have to test a bit with other browsers, but I figured I'd ask here to see if there are any quick answers.

Any ideas?

Sekka

2:39 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



I have a similar system but I use preg_replace to remove any characters I deem "invalid". For example,

$mynewfilename = preg_replace ("/[^a-zA-Z0-9\_]/", "", $row['_filename']);

The regular expression searches the file name, and if a character is not a letter, number or underscore, it is removed.

An easier method would be to str_replace for the periods and replace them with an underscore, but the regular expression handles all characters you don't want.

cmarshall

2:45 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I may end up doing that.

I have to make sure that I don't remove the last period, though.

dreamcatcher

2:46 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could always get the extension of the file, rename the file and append the existing extension.

dc

RonPK

4:43 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmarshall, does it help if you put the filename between quotes? ie
header("Content-Disposition: filename='" . $row['_filename'] . "'");

cmarshall

5:00 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmarshall, does it help if you put the filename between quotes? ie
header("Content-Disposition: filename='" . $row['_filename'] . "'");

Probably not.

I was looking at the IETF RFC [ietf.org], and it seems rather light on guidance.

I implemented a replacement function, like so:

$fext = preg_replace ( "/.+\.(.*$)/", "$1", $row['_filename']);
$fname = preg_replace ( "/(.+)\..*$/", "$1", $row['_filename']);
$fname = preg_replace ( "/[^a-zA-Z0-9_\-]/", "_", $fname);
header("Content-type: ".$result['sf_mime_type']);
header("Content-disposition: filename=$fname.$fext");
readfile($src_file);

This works, but it does modify the original filename, which I was hoping to avoid.

cmarshall

5:05 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I stand corrected.

If I use double quotes, it works.

Single quotes become part of the filename.

To wit:

header("Content-disposition: filename=\"".$row['_filename']."\"");