Forum Moderators: coopster
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?
$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, 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.