Page 1 of 1

How can we get extension of uploaded image ?

Posted: Sun Jul 06, 2008 9:20 am
by Mds
Hi.
I want to get extension of uploaded image.
Can you guidance me ?

Re: How can we get extension of uploaded image ?

Posted: Sun Jul 06, 2008 10:12 am
by s.dot
You don't really want to go for the extension. You want to analyze what type of image it is and make your own extension.

For example,

Code: Select all

if (!empty($_FILES['fieldname']['tmp_name']))
{
    if (!$imgInfo = @getimagesize($_FILES['fieldname']['tmp_name']))
    {
        die('You did not upload an image.');
    } else
    {
        switch ($imgInfo[2])
        {
            case 1:
            $imgExtension = '.gif';
            break;
 
            case 2:
            $imgExtension = '.jpg';
            break;
 
            case 3:
            $imgExtension = '.png';
            break;
 
            //etc
        }
    }
}
Hope this helps :)

Re: How can we get extension of uploaded image ?

Posted: Sun Jul 06, 2008 3:00 pm
by Mds
Thank you very much.
I have a question :

What is it in your code : $imgInfo[2]

Re: How can we get extension of uploaded image ?

Posted: Sun Jul 06, 2008 7:48 pm
by s.dot
$imgInfo is the array returned by getimagesize()

[0] index returns the width of the image
[1] index returns the height of the image
[2] index returns the type of image (we wanted the type to determine the extension)

[3] and [4] contain some more information about the image as well

Re: How can we get extension of uploaded image ?

Posted: Tue Jul 08, 2008 7:12 am
by Mds
Thank you my friend . :D