How do I get a file extension in PHP?

前端 未结 28 2290
一向
一向 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:01
    substr($path, strrpos($path, '.') + 1);
    
    0 讨论(0)
  • 2020-11-21 23:02

    pathinfo is an array. We can check directory name, file name, extension, etc.:

    $path_parts = pathinfo('test.png');
    
    echo $path_parts['extension'], "\n";
    echo $path_parts['dirname'], "\n";
    echo $path_parts['basename'], "\n";
    echo $path_parts['filename'], "\n";
    
    0 讨论(0)
  • 2020-11-21 23:03

    People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP (I am looking at Pythonistas right now :-)).

    In fact, it does exist, but few people know it. Meet pathinfo():

    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    

    This is fast and built-in. pathinfo() can give you other information, such as canonical path, depending on the constant you pass to it.

    Remember that if you want to be able to deal with non ASCII characters, you need to set the locale first. E.G:

    setlocale(LC_ALL,'en_US.UTF-8');
    

    Also, note this doesn't take into consideration the file content or mime-type, you only get the extension. But it's what you asked for.

    Lastly, note that this works only for a file path, not a URL resources path, which is covered using PARSE_URL.

    Enjoy

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

    There is also SplFileInfo:

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

    Often you can write better code if you pass such an object around instead of a string. Your code is more speaking then. Since PHP 5.4 this is a one-liner:

    $ext  = (new SplFileInfo($path))->getExtension();
    
    0 讨论(0)
  • 2020-11-21 23:04

    E-satis's response is the correct way to determine the file extension.

    Alternatively, instead of relying on a files extension, you could use the fileinfo to determine the files MIME type.

    Here's a simplified example of processing an image uploaded by a user:

    // Code assumes necessary extensions are installed and a successful file upload has already occurred
    
    // Create a FileInfo object
    $finfo = new FileInfo(null, '/path/to/magic/file');
    
    // Determine the MIME type of the uploaded file
    switch ($finfo->file($_FILES['image']['tmp_name'], FILEINFO_MIME)) {        
        case 'image/jpg':
            $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
        break;
    
        case 'image/png':
            $im = imagecreatefrompng($_FILES['image']['tmp_name']);
        break;
    
        case 'image/gif':
            $im = imagecreatefromgif($_FILES['image']['tmp_name']);
        break;
    }
    
    0 讨论(0)
  • 2020-11-21 23:05

    Actually, I was looking for that.

    <?php
    
    $url = 'http://example.com/myfolder/sympony.mp3?a=1&b=2#XYZ';
    $tmp = @parse_url($url)['path'];
    $ext = pathinfo($tmp, PATHINFO_EXTENSION);
    
    var_dump($ext);
    
    0 讨论(0)
提交回复
热议问题