frapet
frapet

Reputation: 229

PHP upload pics with space

I have an upload pictures form. I need to make sure that pictures which contain spaces in the name are replaced with something else, my guess is %.

ie the picture's name is: aa 22.jpg

If uploaded this way it does not show because of the space between aa and 22. What is the way to get rid of the space or replace it with % when uploaded?

Upvotes: 2

Views: 2699

Answers (4)

joe
joe

Reputation: 11

you can use trim($picture), but dont know where to put it, finding out my self right now

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382806

rather than replacing file name with empty string, it is good idea to replace it with an underscore to maintain the readability and actual name of the file in its sense:

$file = str_replace(' ', '_', $file);

Upvotes: 6

joukokar
joukokar

Reputation: 73

If the file uploads correctly but just doesn't show up in the browser, you might try aa%2022.jpg, since %20 is used instead of spaces in URLs (and iirc some browser won't realize this automatically).

But since it's ugly, you might prefer replacing spaces with something else. You can use str_replace function to achieve this.

Upvotes: 0

Langdon
Langdon

Reputation: 20073

Can't you just...

$filename = str_replace(' ', '', $filename);

Are you using some upload handler that you don't have control over?

Upvotes: 3

Related Questions