How do I get a file extension in PHP?

前端 未结 28 2294
一向
一向 2020-11-21 22:45

This is a question you can read everywhere on the web with various answers:

$ext = end(explode(\'.\', $filename));
$ext = substr(strrchr($filename, \'.\'), 1         


        
相关标签:
28条回答
  • 2020-11-21 23:17
    ltrim(strstr($file_url, '.'), '.')
    

    this is the best way if you have filenames like name.name.name.ext (ugly, but it sometimes happens

    0 讨论(0)
  • 2020-11-21 23:20

    As long as it does not contain a path you can also use:

    array_pop(explode('.', $fname))
    

    Where $fname is a name of the file, for example: my_picture.jpg. And the outcome would be: jpg

    0 讨论(0)
  • 2020-11-21 23:21

    Example URL: http://example.com/myfolder/sympony.mp3?a=1&b=2#XYZ

    A) Don't use suggested unsafe PATHINFO:

    pathinfo($url)['dirname']                                                                       
    0 讨论(0)
  • 2020-11-21 23:21

    1) If you are using (PHP 5 >= 5.3.6) you can use SplFileInfo::getExtension — Gets the file extension

    Example code

    <?php
    
    $info = new SplFileInfo('test.png');
    var_dump($info->getExtension());
    
    $info = new SplFileInfo('test.tar.gz');
    var_dump($info->getExtension());
    
    ?>
    

    This will output

    string(3) "png"
    string(2) "gz"
    

    2) Another way of getting the extension if you are using (PHP 4 >= 4.0.3, PHP 5) is pathinfo

    Example code

    <?php
    
    $ext = pathinfo('test.png', PATHINFO_EXTENSION);
    var_dump($ext);
    
    $ext = pathinfo('test.tar.gz', PATHINFO_EXTENSION);
    var_dump($ext);
    
    ?>
    

    This will output

    string(3) "png"
    string(2) "gz"
    

    // EDIT: removed a bracket

    0 讨论(0)
  • 2020-11-21 23:21

    I found that the pathinfo() and SplFileInfo solutions works well for standard files on the local file system, but you can run into difficulties if you're working with remote files as URLs for valid images may have a # (fragment identifiers) and/or ? (query parameters) at the end of the URL, which both those solutions will (incorrect) treat as part of the file extension.

    I found this was a reliable way to use pathinfo() on a URL after first parsing it to strip out the unnecessary clutter after the file extension:

    $url_components = parse_url($url); // First parse the URL
    $url_path = $url_components['path']; // Then get the path component
    $ext = pathinfo($url_path, PATHINFO_EXTENSION); // Then use pathinfo()
    
    0 讨论(0)
  • 2020-11-21 23:23

    $ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $fileName);

    preg_replace approach we using regular expression search and replace. In preg_replace function first parameter is pattern to the search, second parameter $1 is a reference to whatever is matched by the first (.*) and third parameter is file name.

    Another way, we can also use strrpos to find the position of the last occurrence of a ‘.’ in a file name and increment that position by 1 so that it will explode string from (.)

    $ext = substr($fileName, strrpos($fileName, '.') + 1);

    0 讨论(0)
提交回复
热议问题