513

I wish to get the file extension of an image I am uploading, but I just get an array back.

$userfile_name = $_FILES['image']['name'];
$userfile_extn = explode(".", strtolower($_FILES['image']['name']));

Is there a way to just get the extension itself?

Community
  • 1
  • 1
Keith Power
  • 13,891
  • 22
  • 66
  • 135

5 Answers5

1319

No need to use string functions. You can use something that's actually designed for what you want: pathinfo():

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • $path = $_FILES['image']['name'][0]; the actual file name is in an array. pathinfo will only evaluate a string. – foureight84 Dec 04 '13 at 22:36
  • 11
    @foureight84: That's only the case if you have upload fields named `image[]`... – ThiefMaster Dec 04 '13 at 23:42
  • 2
    I kinda modified your code a little and made a function, in case someone wants to use a function. Here is the code:function getFileExtension($path) { $ext = pathinfo($path, PATHINFO_EXTENSION); return $ext; } – Amir Md Amiruzzaman Dec 05 '14 at 14:56
  • 3
    @ThiefMaster this returns only the FIRST extension though. In the case of images this may not be relevant, but when someone's uploading an archive it could be .tar.gz or something... – user1914292 Apr 24 '15 at 17:48
  • 6
    @user1914292 I don't see how that is relevant, you should always modify code for own use instead of just copying it and expecting it to work. so if you are expecting archived files instead of images you should make your code work with that too (and not just copy this answer and expect it to work for you) – xorinzor Jan 15 '17 at 15:00
  • This method returns GET parameters as well.... – Pinonirvana Jan 03 '21 at 18:35
67

This will work as well:

$array = explode('.', $_FILES['image']['name']);
$extension = end($array);
The Alpha
  • 143,660
  • 29
  • 287
  • 307
Andrey
  • 815
  • 6
  • 2
  • 19
    'Only variables should be passed by reference', the PHP Manual – dader Dec 20 '12 at 02:01
  • 1
    This is not a good approach since the `end` function is intended to move the internal pointer of an array to the end of the array and return that element. The problem is that the array is passed by reference and since your array is dynamically generated, it cannot be passed by reference, hence the warning or failure, depending on your version of PHP. – SteveK May 19 '13 at 02:27
  • 33
    **Important Note**: This will fail with paths like: `/var/www/website.com/somefile` (you're better off using pathinfo). – brianreavis Nov 27 '13 at 21:30
  • This will fail when you changed a file extention on Windows from .png to .jpg. Under water it will always be a .png. – CodeWhisperer Mar 25 '19 at 13:51
37

A better method is using strrpos + substr (faster than explode for that) :

$userfile_name = $_FILES['image']['name'];
$userfile_extn = substr($userfile_name, strrpos($userfile_name, '.')+1);

But, to check the type of a file, using mime_content_type is a better way : http://www.php.net/manual/en/function.mime-content-type.php

user3942918
  • 25,539
  • 11
  • 55
  • 67
Julien
  • 1,296
  • 1
  • 9
  • 12
24

You could try with this for mime type

$image = getimagesize($_FILES['image']['tmp_name']);

$image['mime'] will return the mime type.

This function doesn't require GD library. You can find the documentation here.

This returns the mime type of the image.

Some people use the $_FILES["file"]["type"] but it's not reliable as been given by the browser and not by PHP.

You can use pathinfo() as ThiefMaster suggested to retrieve the image extension.

First make sure that the image is being uploaded successfully while in development before performing any operations with the image.

Balan
  • 241
  • 1
  • 3
12

How about

$ext = array_pop($userfile_extn);
ilanco
  • 9,581
  • 4
  • 32
  • 37
  • 9
    I don't recommend this. You are relying on the order of the array being the same years from now. It's a lazy solution that will most likely cause issues in the future. – Wade Jul 29 '16 at 20:14