View Full Version : Removing characters from an uploaded file

03-13-2007, 01:29 AM
I have a system whereby a user can upload a picture onto my server, I have this check in place to make sure they must upload a .jpg

if ($i[2]==2) {
$errortype = 0;
} else {
$errortype = 1;

My problem is that, if the filename contains a ' the whole system gets messed up, the apostrophe is causing the problem, is there anyway to remove it/all dissallowed characters from the filename and still run the above check on the file?


03-13-2007, 02:07 AM
The name can be parsed with the addslashes() function, if you would like to keep it. Otherwise, I'd strip it using str_replace("'", "", $input);

03-13-2007, 06:30 AM
tmp_name shouldn't contain any apostrophes at all, it should just be text. Are you sure the problem is there? Incidentally you can remove all characters that aren't allowed by either hashing the filename or using regular expressions. preg_replace() (http://www.php.net/preg_replace) or ereg_replace() (http://www.php.net/ereg_replace).

03-13-2007, 10:47 AM
How would I go about hashing the filename? that would be a pretty good idea.

I'm pretty sure the problem is with the apostrophes in filename, but along with this check there is a script that makes 2 dynamic thumbnails and another that resizes the uploaded image. Everything works fine except when there is an apostrophe in the filename.

how would i change


to show the new hashed file name?