Click to See Complete Forum and Search --> : PHP Upload -> filenames


aj_nsc
03-29-2007, 08:37 AM
When I am trying to upload a file like "Gord's Gold.jpg" the filename comes out as "s Gold.jpg".....anything I can do about this? I don't know where to even begin because the filename gets chopped off by the $_FILES array before I can even get my hands on it. Advice is appreciated.

bokeh
03-29-2007, 08:57 AM
Give your files proper names to start with.

aj_nsc
03-29-2007, 09:19 AM
These are files that will be uploaded by users, I have no control over what they want to call them. In any case, I am guessing your reply is implying that there is nothing I can do. Anybody want to verify that assumption?

DARTHTAMPON
03-29-2007, 09:36 AM
You can rename the files at your pleasure.

On a side note you could convert the file name part of the file to web safe syntax. I forget the conversion method name but im sure some of the guru's can help out with that, I will look around and see if I can find it.

DARTHTAMPON
03-29-2007, 09:43 AM
ok Im not sure this is the best way to go about it but might set you in the right direction

you have "Gord's Gold.jpg"

chop off and remember the extension.

so you now have "Gord's Gold"

$file = htmlspecialchars("Gord's Gold");
then rename the file to $file.$extension;

This will make the title display like you want it to but eliminate all of the bad code that goes along with it.

aj_nsc
03-29-2007, 10:43 AM
Thanks to bokeh's helpful advice, I did a little more digging and came up with a way to do what I want. I figure I'd post it here in case anyone else comes across the problem.

To get the correct filename from an upload which may contain anything for the filename, then I used a combination of Javascript and php (I came up with the methodology on my own, but the php code I got from http://www.php.net/features.file-upload .

Before uploading a file, take the complete file path and pass it to a hidden field via javascript. When uploading the file, grab the filename from the hidden $_POST field that you put the file path in and use this code


$filename = addslashes(basename(stripslashes($_POST['full_file_path'])));


That makes it ready for a mysql_query, if you just want the filename, then get rid of the addslashes function.