PHP file extension issue [duplicate]

一曲冷凌霜 提交于 2019-12-08 09:43:19

问题


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


回答1:


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;
       } 




回答2:


You can use pathinfo for this:

echo pathinfo("foo.txt", PATHINFO_EXTENSION);

But rolling your own with strrpos is not difficult as well.




回答3:


Yes, using the Pathinfo() function.

<?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
?>

The above example will output:

/www/htdocs/inc
lib.inc.php
php //your extension
lib.inc



回答4:


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 ;)



来源:https://stackoverflow.com/questions/12931296/php-file-extension-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!