Author |
Message |
guidyy
Worker


Joined: Nov 22, 2004
Posts: 208
Location: Italy
|
Posted:
Mon Apr 14, 2008 3:22 am |
|
Anyone know of a function that transform a malformed uri?
I have people uploading photos or filenames like my new file.txt
or cool photo.jpg.
I'd like to change to my_new_file.txt or cool_photo.jpg before saving it to the server.
Thanks.
Guido |
|
|
|
 |
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Mon Apr 14, 2008 5:48 am |
|
You could use str_replace, or one of the ereg or preg_replace... You have to write the replacements though... |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
 |
guidyy

|
Posted:
Mon Apr 14, 2008 6:00 am |
|
That's the problem! I sux with regexp!  |
|
|
|
 |
montego

|
Posted:
Mon Apr 14, 2008 6:16 am |
|
Question: does the URI need to retain its name or can it be a hashed name? The reason I ask is that it might actually be safer to hash the name anyways and use it instead. You would pre-pend or append a salt value onto it and then do an MD5 hash on it. Then, just in case, check to see if the file exists (just a precautionary step although it would be highly unlikely).
If you really need to retain the original file name, you could potentially do this with the database and then stream the file back to the end user using that name.
If none of the above makes sense or is not desired, and you don't want to do the regex's yourself, you could still use str_replace, a series of them. But, ereg_replace/preg_replace are really your best bet. You might be able to find the regex's already done for you with some searching in Google. |
|
|
|
 |
guidyy

|
Posted:
Mon Apr 14, 2008 6:33 am |
|
It can be so stupid!
I did
Code:
$name="baked macaroni and cheese.jpg";
$name=str_replace(" ","_", $name);
echo $ name;
|
and I got baked_macaroni_and_cheese.jpg
I'd like to retain the file name for SEO purpose so images have more choices to be indexed. search.live.com has the best image search, but they do care a lot about keyword in filename and alt tag.
Why am I so stupid at times? |
|
|
|
 |
montego

|
Posted:
Mon Apr 14, 2008 6:52 am |
|
You might have to keep an eye on the file names as there could be other characters that you might end up wanting to "trap" for. For example, not sure if you would want the following right?
baked%20macaroni%20and%20cheese.jpg
Just something to keep an eye on I guess. |
|
|
|
 |
guidyy

|
Posted:
Mon Apr 14, 2008 9:27 am |
|
Right.. Guess I need to create an array of crap_to_avoid_in_url and make a check just after the submission form. |
|
|
|
 |
Gremmie
Former Moderator in Good Standing

Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA
|
Posted:
Mon Apr 14, 2008 9:41 am |
|
str_replace() can take an array for the search argument (and replace argument). So if you wanted to replace all the "bad" stuff with "_" you could do:
Code:
$badStuff = array(' ', '%20', etc);
$s = str_replace($badStuff, '_', $s);
|
|
_________________ Only registered users can see links on this board! Get registered or login! - An Event Calendar for PHP-Nuke
Only registered users can see links on this board! Get registered or login! - A Google Maps Nuke Module |
|
|
 |
guidyy

|
Posted:
Mon Apr 14, 2008 9:56 am |
|
works like a charm! ...... |
|
|
|
 |
gotcha
Regular


Joined: Mar 14, 2005
Posts: 91
|
Posted:
Mon Apr 14, 2008 10:42 am |
|
this will make it a bit safer and only allows letters, numbers, _, -, and . in the filename after converting the original to lowercase..
Code:$clean_filename = preg_replace('/[^a-z0-9_\-\.]/i', '_', strtolower($original_filename))
|
|
|
|
|
 |
|