How can we get extension of uploaded image ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

How can we get extension of uploaded image ?

Post by Mds »

Hi.
I want to get extension of uploaded image.
Can you guidance me ?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How can we get extension of uploaded image ?

Post 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 :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

Re: How can we get extension of uploaded image ?

Post by Mds »

Thank you very much.
I have a question :

What is it in your code : $imgInfo[2]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How can we get extension of uploaded image ?

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

Re: How can we get extension of uploaded image ?

Post by Mds »

Thank you my friend . :D
Post Reply