The answer posted by @jereon is correct, and the easiest way to do this.
However, if you would like to extract more information about the file without using explode and end, you can simply use the PHP built in pathinfo()
function.
Reference: pathinfo()
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
This will return
/www/htdocs/inc
lib.inc.php
php
lib.inc
As $filename
is an array, you need to loop through it in order to read it into pathinfo()
. This can be done very easily using a foreach loop like so:
// Loop through the filenames array
foreach($filename as $value){
// Retrieve the path parts of each individual file
$path_parts[] = pathinfo($value);
}
// Write out the path parts array
print_r($path_parts);
5
solved Get Php file/images extension