0

I am getting a problem to check wheather image is png,gif,jpg,jpeg. i tried using getimagesize(path to file) but it needs full path of image as i just have only image name that is image1.jpg. I want to check file type that is png,gif,jpg,jpeg before uploading it. I want a code to check type before uploading it to the directory. getimagesize(path to file) gives me waring that Failed to open stream becaus efile is not in directory, it is not uplaoded yet. I didnt find any thing that works for me. Can anybody here help me out of this problem. Here is my code:

list(image_width, image_height, image_type) = getimagesize("image1.jpg");
switch ($source_image_type)
{
    case IMAGETYPE_GIF:
        //some code here
    break;
    case IMAGETYPE_JPEG:
        //some code here
    break;
    case IMAGETYPE_PNG:
        //some code here
    break;
}

This code gives me warning that Failed to open stream.

Simranjeet Kaur
  • 259
  • 1
  • 6
  • 18
  • Before uploading? So you want to check if the image is of the correct extension before upload, you'll need to use an html5 element that filters the file element. If you mean before moving the uploaded file to a non-temp folder, then you can use [`exif_imagetype`](http://php.net/manual/en/function.exif-imagetype.php). – Dave Chen Sep 02 '14 at 04:59
  • It gives me warning that "Failed to open stream". As the file is not uploaded in the directory. i want a code to check the type before uploading it. – Simranjeet Kaur Sep 02 '14 at 05:11
  • If it's before uploading, then you only have the filename to work with. Best bet is to explode and check the last extension -- which is exactly what 웃웃웃웃웃 does. – Dave Chen Sep 02 '14 at 05:13
  • I think you can't trust the upload when you only check the mime type on client side (before uploading). Anyway.. you can check it with JavaScript like so http://jsfiddle.net/634e0mc1/1/ . The `accept` attribute is also helpful `` – sofl Sep 02 '14 at 15:05

2 Answers2

0

Use the following function :

string image_type_to_mime_type ( int $imagetype );

Kindly refer this link.

Dinesh Patil
  • 615
  • 2
  • 15
  • 37
0

in php manual there is a build in function to get image type, you can use exif_imagetype() and do what ever you wanted to do,

int exif_imagetype ( string $filename )

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

Windows users: If you get the fatal error "Fatal error: Call to undefined function exif_imagetype()", and you have enabled php_exif.dll, make sure you enable php_mbstring.dll. You must put mbstring before exif in the php.ini, i.e.:

extension=php_mbstring.dll
extension=php_exif.dll

You can check whether this has worked by calling phpinfo() and searching for exif. 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;
    }
}

if you want to check before uploading an image, you can use

if($_FILES['file_upload']['type'] != 'image/png'){
    die('Unsupported filetype uploaded.');
}
Khairu Aqsara
  • 1,321
  • 3
  • 14
  • 27
  • It gives me warning that "Failed to open stream". As the file is not uploaded in the directory. I want somthing that will check the image type before uploading it. – Simranjeet Kaur Sep 02 '14 at 05:08
  • edited my answer, look at the bottom, i think, it can be done by using regular function from uploading image – Khairu Aqsara Sep 02 '14 at 05:19