Page 1 of 1
str_replace image names
Posted: Wed Mar 22, 2006 4:42 pm
by ssand
I ran into a problem when I uploaded an image file with a space in the name. At page load the pulled image name from the database stops at the space.
I would like to correct the problem when the images are added to the database.
I added str_replace but should urlencode or htmlentities instead? If by chance some other spec character got in the file name?
Code: Select all
$_FILES['userfile']['name'] = str_replace(" ", "_", $_FILES['userfile']['name']);
Posted: Wed Mar 22, 2006 4:46 pm
by feyd
It may be more of a problem in how you are using the filename. Could you post the code where the filename is inserted into a tag? Basically, what I'm getting at is if you don't use quotes around your attribute values, this sort of thing can and will happen. However, your server may prefer to get all requests encoded, in which case,
rawurlencode() and
urlencode() can be used.
Posted: Wed Mar 22, 2006 5:04 pm
by ssand
Here's how the images are called:
Code: Select all
<img src=images/thumbnail/$inventory_list_qry[thumbphoto] border=0>
Actually I changed it to this for a test. Uploaded an image with spaces and the image worked.
Code: Select all
<img src=\"images/thumbnail/".$inventory_list_qry['thumbphoto']."\" border=0>
Maybe I need to do both. Add the underscore to spaces and fix how I call the images.
Posted: Thu Mar 23, 2006 9:25 am
by pickle
You should ALWAYS wrap your attribute values in quotes. Not only is it required for standards compliance, but it also makes the code easier to read (IMO), and of course stops problems like this from happening.
Posted: Thu Mar 23, 2006 9:46 am
by fastfingertips
My advice is to use preg_replace because depending on the OS you may need to alter the rules of special chars that need replacement and you may use something like:
Code: Select all
$arrPattern['space'] = " ";
$arrPattern['line'] = "-";
$arrReplace['space']="_";
$arrReplace['line'] = "_";
$_FILES['userfile']['name']= preg_replace($arrPattern,$arrReplace,$_FILES['userfile']['name']);
Posted: Mon Mar 27, 2006 9:13 am
by ssand
Thanks for the replies.
Good idea fastfingers, I will insert that. Would make it easier to add chars, etc.
Thanks.
