Saturday, September 12, 2015

PHP get extension of a file

PHP supports file uploads. user can design a system where he can allow users to upload files with specific extension type.
In many website we have the provision to upload files with specific file extension.
For example facebook we can upload file with extension jpeg, jpg,png etc for the profile picture.

Similarly it can be achieved in PHP. Following function returns you the extension of the given file type.

function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$filename = "images/image01.jpeg" ;
$extension=getExtension($filename);
echo "Extension of the file is: ".$extension;

The output of the above function call will be:

Extension of the file is: jpeg

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...