0

I'm trying to fix a picture upload. But I have some troubles with the check if the file is a picture. Before I used eregi_replace but that function is deprecated and now I'm wondering is there possible to use preg_match and check in an array for a match or how should I do this?

    $name = "Picture.jpg";
    $picture = array("/^JPG/", "/^PNG/", "/^GIF/", "/^gif/", "/^png/", "/^jpg/", "/^JPEG/", "/^jpeg/");
    $something = preg_match($picture, $name, $matches, PREG_OFFSET_CAPTURE, 3); 
    print_r($something);

Like that or something? I dont have a clue, hope you can help me!

Ashoka
  • 452
  • 1
  • 5
  • 20
Tommy
  • 667
  • 1
  • 10
  • 25
  • I'd check this answer instead, checking on actual MIME-type/fileinfo is better http://stackoverflow.com/questions/310714/how-to-check-file-types-of-uploaded-files-in-php – jtheman Feb 14 '14 at 09:02
  • 1
    That is a horribly BAD way of checking for "imageness" of an upload. File extensions are handy for quick identification, but they're so utterly trivial to forge. You should be using better methods, such as `fileinfo` for mime-type determination. – Marc B Feb 14 '14 at 09:03

1 Answers1

1

Like below:

$name = "Picture.jpg";

if (preg_match('/\.(jpe?g|png|gif)$/i', $name, $matches)) {
  //...
}

But note this only check the filename, if you want to check it's a real image, you should validate the actual mime-type.

xdazz
  • 158,678
  • 38
  • 247
  • 274