views:

239

answers:

3

I'm using Uploadify to upload an image to the server.
The image is uploaded, and placed in the temp folder of the web server.

Now I need to work with the file beore moving it to it's real location, and I have the following code:

// Get the filepath and filename from server temp dir
$sourceFile   = $_FILES[ 'Filedata' ][ 'tmp_name' ]; // e.g. c:\server\path\tmp\php159.tmp

// Solution 1 for getting file extension
$fileExt1     = pathinfo($sourceFile, PATHINFO_EXTENSION); // <-- This only returns .tmp

// Solution 2 with getimagesize
list(,,$extension) = getimagesize($sourceFile);
$fileExt2     = $extension; // this only returns the number 2.

// Solution 3 with getimagesize
$img = getimagesize($sourceFile);
$fileExt3 = $img[2]; // this only returns the number 2.

I'm not using regex to read filename, because a user may name the file anything, so I have to read file data.

Any sugegstions anyone?

+1  A: 

exif_imagetype() can determine a number of graphics formats, is fast and doesn't even use GD.

Edit: I see you are already using getimagesize() and have trouble translating the constants. The exif_imagetype() page linked above has a translation of all returned image types that should be valid for getimagesize() as well.

Pekka
I only get `Call to undefined function exif_imagetype()`. Others have similar problems: http://www.ivorde.ro/PHP_Error_Call_to_undefined_function_exif_imagetype__on_FreeBSD-35.html and http://www.wampserver.com/phorum/read.php?2,34044
Steven
thanx for the information.... this is really good. it determines the image type using image header (MIME type), so even if a GIF file has some other extension it will detect that image as GIF image type
Pragati Sureka
@Steven what version of PHP are you using. This function is for PHP 4.3.0 and above.
Pragati Sureka
@Sureka: exif has to be compiled with PHP, and that is not always the case.
Alix Axel
+1  A: 

Your solutions #2 and #3 both work already. From the getimagesize() manual page:

Returns an array with 7 elements.
...
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

So for a .gif then this should be the integer 2. To get the file extension use image_type_to_extension() since you already are using the gd library. eg.

$img = getimagesize($sourceFile);
$fileExt = image_type_to_extension($img[2])
bearver
+1 for the image_type_to_extension(), however IMAGETYPE_GIF = 1, not 2.
Alix Axel
Thanks for pointing that out.
bearver
+1  A: 

Well, first off $sourceFile should be $_FILES['Filedata']['name'] instead of $_FILES['Filedata']['tmp_name'] but only for your first solution.

Now regarding your solutions/problems:

  1. pathinfo($sourceFile, PATHINFO_EXTENSION); // should work now
  2. getimagesize() returns a constant indicating the image type in the second index
  3. same as point 2

Keep in mind that exif_imagetype() returns exactly the same information as the second index of getimagesize(), if you have access to this function it should perform way better.

Now for the image constants, the most common three are:

  • IMAGETYPE_GIF = 1
  • IMAGETYPE_JPEG = 2
  • IMAGETYPE_PNG = 3

Checking is as simple as doing something like this:

switch (exif_imagetype($_FILES['Filedata']['tmp_name']))
{
    case IMAGETYPE_GIF:
        echo 'is a GIF';
    break;

    case IMAGETYPE_JPEG:
        echo 'is a JPEG';
    break;

    case IMAGETYPE_PNG:
        echo 'is a PNG';
    break;

    case default:
        echo 'is something else;
    break;
}

One more thing, it's better to use getimagesize() / exif_imagetype() than to rely on the file extension, since the extension can be easily changed.

Alix Axel
Ah, so THAT's what the 2 means :) Thanks.
Steven
I can't use exif_imagetype, at least not on my own computer. It hasn't been compiled with my PHP (I'm using v5.x). I only get `Call to undefined function exif_imagetype()`. (see links in my comment below)
Steven