找到给定文件名的文件扩展名

时间:2013-10-04 10:50:15

标签: php

如何查找给定文件名的扩展名:

见下面的代码:

  

$ file_name =“sample.jpg”;

Output will be: .jpg

我需要找到文件名的扩展名

示例:如果我们解析sample.jpg,我应该返回.jpg

3 个答案:

答案 0 :(得分:0)

您可以尝试使用爆炸

    $file_name = "sample.jpg";
    $pieces = explode(".", $file_name);
    echo $pieces[0]; // sample
    echo '.'.$pieces[count($pieces)-1]; // .jpg

您也可以使用:

$ext = pathinfo($file_name, PATHINFO_EXTENSION);
echo $ext;//jpg

答案 1 :(得分:0)

这里有一种查找方式:

<?php
$filename = "sample.txt.jpg";
$ext = substr($filename, strrpos($filename, "."));
echo $ext;
?>

答案 2 :(得分:0)

使用PHP SPL类查找有关文件的任何信息。更多细节read here

$file = new SplFileInfo($path);
$ext  = $file->getExtension();