How to get the file extension? [duplicate]

2019-08-04 18:26发布

Possible Duplicate:
How to extract a file extension in PHP ?

For example if i have a path (generated by my torrent_info_gen)-

Driver Genius Professional Edition 10.0.0.526/Driver Genius Professional Edition 10.0.0.526.exe

How should i get the extension of the file in the path! I also want to have the path and filename to be shown differently in this way:

Path: (Path of the file)

Filename: (Name of the file)

Extension: (Extension of the file)

Can someone help me! Please!!!!!

Thanks in advance!

标签: php file
3条回答
ゆ 、 Hurt°
2楼-- · 2019-08-04 18:47

the obvious solution is already posted so here is smthing different

      $file = 'C:/foo/bar/do.exe';
    echo dirname($file);
    $ext = array_pop(explode('.',$file));
    $filename = basename($file);
$filenameWithoutExtension = basename($file,'.' . $ext);

    echo $ext;
    echo $filename;
查看更多
Anthone
3楼-- · 2019-08-04 18:49
$info = pathinfo($filename);

echo "Path: ".$info['dirname'];
echo "Filename: ".$info['basename'];
echo "Extension: ".$info['extension'];

Documentation on pathinfo.

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-04 18:54

What about this:

    $str = "Driver Genius Professional Edition 10.0.0.526/Driver Genius Professional Edition 10.0.0.526.exe";

    $info  = pathinfo($str);

    var_dump($info['dirname']);
    var_dump($info['filename']);
    var_dump($info['extension']);

Gives:

string 'Driver Genius Professional Edition 10.0.0.526' (length=45)
string 'Driver Genius Professional Edition 10.0.0.526' (length=45)
string 'exe' (length=3)
查看更多
登录 后发表回答