PHP Obtener la extensión del archivo desde el nombre de archivo
$ext = pathinfo($filename, PATHINFO_EXTENSION);
Geeky Bravo
$ext = pathinfo($filename, PATHINFO_EXTENSION);
// your file
$file = 'image.jpg';
$info = pathinfo($file);
// Before PHP 5.2
$file_name = basename($file, '.'.$info['extension']);
// After PHP 5.2
$file_name = $info['filename'];
$ext = explode('.', $filename); // Explode the string
$my_ext = end($ext); // Get the last entry of the array
echo $my_ext;
/* 387 ns */ function method1($s) {return preg_replace("/.*\./","",$s);} // edge case problem
/* 769 ns */ function method2($s) {preg_match("/\.([^\.]+)$/",$s,$a);return $a[1];}
/* 67 ns */ function method3($s) {$n = strrpos($s,"."); if($n===false) return "";return substr($s,$n+1);}
/* 175 ns */ function method4($s) {$a = explode(".",$s);$n = count($a); if($n==1) return "";return $a[$n-1];}
/* 731 ns */ function method5($s) {return pathinfo($s, PATHINFO_EXTENSION);}
/* 732 ns */ function method6($s) {return (new SplFileInfo($s))->getExtension();}
// All measured on Linux; it will be vastly different on Windows
function fileExtension($name) {
$n = strrpos($name, '.');
return ($n === false) ? '' : substr($name, $n+1);
}
//Remember this is for simple filenames only. If you have paths involved, stick to pathinfo or deal with the dirname separately.