Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
guidyy
Worker
Worker



Joined: Nov 22, 2004
Posts: 208
Location: Italy

PostPosted: Mon Apr 14, 2008 3:22 am Reply with quote

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
 
View user's profile Send private message Visit poster's website MSN Messenger
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Mon Apr 14, 2008 5:48 am Reply with quote

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! 
View user's profile Send private message Visit poster's website
guidyy







PostPosted: Mon Apr 14, 2008 6:00 am Reply with quote

That's the problem! I sux with regexp! Very Happy
 
montego







PostPosted: Mon Apr 14, 2008 6:16 am Reply with quote

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







PostPosted: Mon Apr 14, 2008 6:33 am Reply with quote

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







PostPosted: Mon Apr 14, 2008 6:52 am Reply with quote

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







PostPosted: Mon Apr 14, 2008 9:27 am Reply with quote

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

PostPosted: Mon Apr 14, 2008 9:41 am Reply with quote

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 
View user's profile Send private message
guidyy







PostPosted: Mon Apr 14, 2008 9:56 am Reply with quote

works like a charm! ......
 
gotcha
Regular
Regular



Joined: Mar 14, 2005
Posts: 91

PostPosted: Mon Apr 14, 2008 10:42 am Reply with quote

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))
 
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©