str_replace image names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

str_replace image names

Post 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']);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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']);
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Thanks for the replies.
Good idea fastfingers, I will insert that. Would make it easier to add chars, etc.

Thanks. :D
Post Reply