1

I have this file that I am uploading to the server via php file upload..

6;'!8.jpg

I used this code to remove non alphanumeric characters..

$image1 = ereg_replace("[^A-Za-z0-9]", "", $_FILES["image"]["name"]);

that works fine, but it returns my image like this

68jpg

What would be the best way to return this as 68.jpg

Any ideas?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user979331
  • 11,039
  • 73
  • 223
  • 418

3 Answers3

2

Don't use ereg/eregi. These functions are deprecated. Use PCRE functions instead.

Try this way

$image1 = preg_replace("#[^a-z0-9\.]#i", null, $_FILES["image"]["name"]);

Karo
  • 744
  • 4
  • 7
0

Try this code instead:

$image1 = ereg_replace("[^A-Za-z0-9\\.]", "", $_FILES["image"]["name"]); 

But you should really try to use preg_ instead of ereg_ PHP functions as they are more performant (and not deprecated).

adrien
  • 4,399
  • 26
  • 26
  • 1
    This still won't work - check Your regexp. It should be with one backslash: `[^A-Za-z0-9\.]`. Your regexp would allow alphanums, a backslash and then any character (cos You did escaped a backslash and an unescaped dot means anything)... – shadyyx May 09 '12 at 15:22
-1

You can probably use PHP's pathinfo($_IMAGE["image"]["name"]) command to break apart a filename's base path, base name and file extension. You can then run your search n' replace (as @John Conde recommends) on the basename, then concatenate the basename and file extension back together.

OrionRogue
  • 398
  • 4
  • 14
  • Why do it this complicated? -1 – shadyyx May 09 '12 at 15:31
  • The problem is that many people solves problems with overcomplicated solutions... Why to use `pathinfo`, then break sth apart and then use the `preg_replace` anyway when it could be used with instant result. – shadyyx May 09 '12 at 15:58