1

Yes as the title says:

How can i detect an file extension? $_POST['fname']; is where i have the filename stored e.g asddsa.jpg

So how can i check if .jpg then ... if .png then ... ?

    $src = "images/status/photo/".$_POST['fname'];
    $parts=pathinfo($src);
if($parts['extension'] == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($parts['extension'] == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($parts['extension'] == "gif"){
    $img_r = imagecreatefromgif($src);
}

tried this too without any success:

    $ext= pathinfo($src, PATHINFO_EXTENSION);
if($ext == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($ext == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($ext == "gif"){
    $img_r = imagecreatefromgif($src);
}
Johnson
  • 818
  • 4
  • 21
  • 39
  • 1
    duplicate of [How to extract a file extension in PHP ?](http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php) – Gordon Nov 01 '10 at 00:08
  • @Gordon, probably but Johnson's question was originally referring to a file name in a string, not an existing file on disk. Looks like he's edited it though and that may have changed. – Brad Nov 01 '10 at 00:18
  • possible duplicate of [PHP/GD - Finding Image Resource Type](http://stackoverflow.com/questions/1965689/php-gd-finding-image-resource-type/1965758#1965758) – Gordon Nov 01 '10 at 00:18
  • @Johnson, `pathinfo()` probably won't work if your file doesn't exist. Does your file exist? – Brad Nov 01 '10 at 00:19
  • @Brad the edit doesnt matter. Getting the filename from a string, regardless of whether the file exists or not, can be solved with the functions given in the first question. And how to determine a MimeType to open the file with GD is answered in the second linked question. So it's a duplicate. – Gordon Nov 01 '10 at 00:27

3 Answers3

14

The proper way to do this would be to use pathinfo on the file. For example...

$parts=pathinfo('somefile.jpg');
echo $parts['extension']; //Returns "jpg"

Now, you're simply looking at a string in a variable, so the way to handle this would be something like this...

$parts=explode(".", $_POST['fname']);
echo $parts[count($parts)-1];

Finally, you should not be detecting file type by extension. This is bad. Instead, you should be using the content-type (commonly referred to as MIME type). You can find this in $_FILES['userfile']['type'] for uploaded files. (documentation)

Once you have your type, you can do a simple switch statement.

Also, beware of folks uploading content that isn't what it says it is. Someone can send files to you with a specific content type, but it may be something such as executable code instead! The only way to be sure is to check the files with your own code server-side. Never trust user input.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • +1, was about to write this. You may also note that is only returns .gz for .tar.gz files, which means it probably do something like `substr($filename, strrpos('.', $filename))` to retrieve the extension. Also note that the MIME type can be modified by the user, so it may not be good to solely base your actions on the MIME type. – Vincent Savard Nov 01 '10 at 00:09
  • Please see my updated question. I did the aprtinfo method, although it do not work properly for me – Johnson Nov 01 '10 at 00:11
  • Technically, .gz is the extension on a .tar.gz file, although we humans don't see it that way. Yes, you are right about that MIME type, especially in the case of where poorly configured computers/browsers might not give you anything specific, but I'd rely on the MIME type long before the file extension, which often mysteriously goes away on OS X. True though the best option would be to check the file contents itself. – Brad Nov 01 '10 at 00:14
  • Johnson, why didn't it work? Can you give us more information. Try a `print_r($parts)` and tell us what you see. Also, if you are running ancient PHP, it may not be available, but I doubt that is the case. – Brad Nov 01 '10 at 00:15
  • Array ( [dirname] => images/status/photo [basename] => 1-886.jpg [extension] => jpg [filename] => 1-886 ) – Johnson Nov 01 '10 at 00:19
  • Without your error message, nobody can help you. Based on your paste there, pathinfo is working fine. – Brad Nov 01 '10 at 00:21
  • so that was right, the extension, but why wont it run my if properly then? – Johnson Nov 01 '10 at 00:21
  • Im just getting Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error... – Johnson Nov 01 '10 at 00:23
  • Then you have an entirely different problem and should post a new question, and be sure to attach the file you are trying to load. Your `if` statement is obviously working fine if you throw in a .jpg and the function imagecreatefromjpeg() runs. – Brad Nov 01 '10 at 00:24
  • update: the if statement works but only with jpg. with print_r i am getting "jpg" when it is a png file, so thats why i am getting that error because it detect it with jpg extension?? but it is a png hmm – Johnson Nov 01 '10 at 00:28
0

You can do something like this:

$aPieces = explode( '.', $_POST[ 'fname' ] ); // A file name can have more than 1 .something

switch( strtolower( $aPieces[ count( $aPieces) - 1 ] ) ) // We take the LAST piece in our array
{
   case 'jpg':

   break;
   case 'png':

   break;
}
dab
  • 817
  • 1
  • 12
  • 23
0

The solution really depends on what you're trying to do, however the simple approach would be something like:

if ( strtolower(substr($_POST['fname'],-3)) == 'jpg' )

You can also switch() on the string instead of using if/else depending on how many comparisons you will be making.

Edit: if you are going to be specifically dealing with images, you're best off using getimagesize

localhost
  • 357
  • 2
  • 8
  • 1
    That would be assuming the file extension is three characters. Most are... but I've seen jpeg files with an actual .JPEG extension. This also assumes that there even is an extension. A better approach would be to find the last period in the file name and check everything after that. dab's solution above is good way, but it again assumes that there is in fact an extension. – pinkfloydx33 Nov 01 '10 at 00:07
  • 1
    ... and what if the extension is less than 3 characters long? `.js`? Question says nothing about only image extensions. – Richard JP Le Guen Nov 01 '10 at 00:07
  • 1
    since all of his examples were images, it's a fair assumption he's handling images. finding the last period is not a viable option either because you can have extensions such as .tar.gz. ultimately it depends on what he is doing, this is just a simple example as he asked a very simple question – localhost Nov 01 '10 at 00:09
  • 1
    @localhost - I'm in an argumentative mood; what about [.ai](http://www.fileinfo.com/common.php)? – Richard JP Le Guen Nov 01 '10 at 00:19
  • @LeguRi lol, nice one. i interpreted this question primarily as handling common image files that someone would typically be uploading to be handled with GD. ultimately it depends on what OP is trying to accomplish. based on how OP edited his original question, it looks like i was correct :) – localhost Nov 01 '10 at 00:22
  • @localhost - You play an impressive game @localhost... this battle is yours... but the war's not over... and I'll be back. :P – Richard JP Le Guen Nov 01 '10 at 00:25