Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

replace a space with an underscore

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

replace a space with an underscore

Postby pugs421 » Fri Jul 04, 2003 7:51 am

*newb* trying to customize a script

I'm sure there is an easier way to accomplish what I want to do but I'm not working with much php knowledge here. Files are being named by the username but I want to replace any spaces with underscores and strip any special charatcers that may cause a problem when retrieving the uploaded file. I keep trying different things (ex. preg_replace) but I cant figure out how to get it to actually work. This is the function that changes the name of the file, if someone could give me a clue that would be great. It almost looks like the time should be added into the filename but it isnt.

Code: Select all
function make_filename($image_name){
   global $HTTP_POST_VARS;
   
   $file_name = time()."_$image_name";        
   $pos = strrpos($file_name, '.');

   $file_name = substr($file_name, 0,$pos).".jpg";
   $file_name   = $HTTP_POST_VARS["img_name"].".jpg";

   return $file_name;
pugs421
New php-forum User
New php-forum User
 
Posts: 31
Joined: Sat Jun 28, 2003 9:09 pm

Postby pootergeist » Fri Jul 04, 2003 9:53 am

you might well be able to do it with urlencode($name) - which would convert any non alphanumeric (except _ underscore) to the %DD equivalent so 452164364321_joe bloggs.jpg would become 452164364321_joe%20bloggs.jpg - which should (afaik) be ok for nix systems.
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby pugs421 » Fri Jul 04, 2003 4:31 pm

Thanks. That took care of the problem with spaces in the filename. Are there any other special characters that would cause problems when displaying an image?
pugs421
New php-forum User
New php-forum User
 
Posts: 31
Joined: Sat Jun 28, 2003 9:09 pm

Postby pootergeist » Fri Jul 04, 2003 11:45 pm

there shouldn't be any problems - best way I guess is to test it - eg make an image and rename it as the urlencode of `!"£$%^&*(&))_-=+/.,';[]{}.jpg or somesuch, then try to access it by doing <img src="%12%25%213%213%23%23%2.jpg"> or whatever.

I'd figure that as long as it can find aaaaa%20bbbb.jpg (ie can suffer the % sign) then it could access any file that only uses alphanumerics, _ and %

Also remember, people on windows systems are restricted as to what symbols they can use in filenames - eg cannot use \ /:*?"<> or |
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron