Possible Duplicate:
How to extract a file extension in PHP?
How can we get file extension with any php function?
Whether there is some built-in function in php. I am using some explode function but any other exists
Yes, you can .......
Just Use the following code.......
Pass path of file to this PHP function and get extension.
function extension($path) { $qpos = strpos($path, "?"); if ($qpos!==false) $path = substr($path, 0, $qpos); $extension = pathinfo($path, PATHINFO_EXTENSION); return $extension; }
You can use pathinfo
for this:
echo pathinfo("foo.txt", PATHINFO_EXTENSION);
But rolling your own with strrpos
is not difficult as well.
Yes, using the Pathinfo()
function.
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "
";
echo $path_parts['basename'], "
";
echo $path_parts['extension'], "
";
echo $path_parts['filename'], "
"; // since PHP 5.2.0
?>
The above example will output:
/www/htdocs/inc
lib.inc.php
php //your extension
lib.inc
This is the code I use to get a file extension:
$filename = '/some/path/file.txt';
$extension = strtolower(substr(strrchr($filename, '.'), 1));
You can wrap it in your Utils
class or something ;)