PHP Web Host - Quality Web Hosting For All PHP Applications Sign up for PayPal and start accepting credit card payments instantly
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
guidyy
Worker
Worker


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

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

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: 7264
Location: Arizona

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

You could use str_replace, or one of the ereg or preg_replace... You have to write the replacements though...
View user's profile Send private message Visit poster's website
guidyy
Worker
Worker


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

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

That's the problem! I sux with regexp! Very Happy
View user's profile Send private message Visit poster's website MSN Messenger
montego
Site Admin


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

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

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


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

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

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


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

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

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


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

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

Right.. Guess I need to create an array of crap_to_avoid_in_url and make a check just after the submission form.
View user's profile Send private message Visit poster's website MSN Messenger
Gremmie
Moderator


Joined: Apr 06, 2006
Posts: 2371
Location: Iowa, USA

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

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


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

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

works like a charm! ......
View user's profile Send private message Visit poster's website MSN Messenger
gotcha
Regular
Regular


Joined: Mar 14, 2005
Posts: 70

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

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

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
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum