0

Possible Duplicate:
How to check if an uploaded file is an image without mime type?
uploading, processing, storing and delivering user-provided files and images

i have image sharing script, i made half of it. the user can rename any php or cgi file to .jpg and upload it and it upload succfully

How to prevent uploading such fake images?

here is my way to check the file type

 $userfile_type = $_FILES['file']['type']; 

and when i rename php file to php.jpg it can be uploaded easly.

Community
  • 1
  • 1
Alamri
  • 2,112
  • 2
  • 16
  • 21

5 Answers5

3
// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME_TYPE);
    $type = finfo_file($finfo, $tmp_name);
finfo_close($finfo);

This is the server giving you the type of the file and not the browser as in your example.

With GD you can do something like getimagesize() which returns zero if the file is not an image

EDIT: "getimagesize is not a GD library function. It's native to PHP" Thanks Vivek

Happy coding !!

kobino
  • 46
  • 3
1

Use imagemagic and check the image info

Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
1

One of the best ways of doing it is using http://php.net/manual/en/ref.fileinfo.php

Sorin Trimbitas
  • 1,467
  • 18
  • 35
1

You can use getimagesize() to check for the image size. False is return if the function is not able to get the size. Even better if you can use GD library and use imagecreatefromstring() function to see whether it is a real image.

vivek
  • 1,944
  • 1
  • 17
  • 26
1

exif_imagetype()

php.net's example :

    if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
        echo 'The picture is not a gif';
    }

also this is directly from the php.net page I linked:

*If the function exif_imagetype() is not available; you can try the following workaround:*

if ( ! function_exists( 'exif_imagetype' ) ) {
    function exif_imagetype ( $filename ) {
        if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) 
        {
            return $type;
        }
    return false;
    }
}
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18