Snippet: Get the file extension Posted as php on Wednesday 20 August 2008 23:25 by RealShadow

Description

Since we are posting snippets, it would be nice if I would have one too, right?

So this is a simple function I use to extract file extension in a cms for a <noname> webpage when user will upload a file.

I have added a check so you can see how it works then. For example, images.


Code (php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

function get_extension($file) {
    $file = explode(".", $file);
    $file = array_reverse($file);
    $file = $file[0];
 
    return $file;
}
 
$allowed = array ("jpg","gif","png");
 
if (!in_array(get_extension($file),$allowed)) {
     print 'Error message.';
}
else {
   // rest of the code
}
 


Replies on Get the file extension :
Jump to comment page: 1

 By Jim on Thursday 21 August 2008 20:54

Nice one mate



Jump to comment page: 1
You are not logged in. Please login or register an account, it just takes 30 seconds.


©Copyrights Combined Minds. All rights reserved 2006 - 2008 : Disclaimer
Realized by www.Minna-Media.com