1

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!

Community
  • 1
  • 1
  • @Mark Cidade: Though the question title implies only an extension, they are looking for more information. not exactly a duplicate, only 1/3 of their question is answered in the other post. – Brad Christie Jan 30 '11 at 05:07
  • There's enough information in the other page (i.e., link to pathinfo docs) to answer this question. – Mark Cidade Jan 30 '11 at 05:23
  • Then why ask a question SO? I mean the docs cover this perfectly... Oh yea, reference; so others with the same problem can have a solution. – Brad Christie Jan 30 '11 at 13:28

3 Answers3

2
$info = pathinfo($filename);

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

Documentation on pathinfo.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

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)
Marcin
  • 215,873
  • 14
  • 235
  • 294
0

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;
Mr Coder
  • 8,169
  • 5
  • 45
  • 74