How to get extension of a file in php

Discussion in 'PHP' started by kaviarasankk, Jul 19, 2010.

  1. #1
    Hello frnds

    input(given): filename without extension
    output(needed) : filename with extension


    thx in advance
     
    kaviarasankk, Jul 19, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    You need to explain more, like where is the input coming from, show an example ?

    ...just a thought though, are you mixing up page names with file names ?
     
    MyVodaFone, Jul 19, 2010 IP
  3. kaviarasankk

    kaviarasankk Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    (input) $filename = thumbs_up;

    (output) echo " thumbs_up(.*)";

    (.*) ->any extension


    while giving filename only as input and i wanna extension of that file as ouput

    (we know the filename, and its directory too)
     
    kaviarasankk, Jul 19, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
    $filename = "somedir/somefile.php";
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    echo $extension;
    ?>
    PHP:
     
    Last edited: Jul 19, 2010
    danx10, Jul 19, 2010 IP
  5. adamsinfo

    adamsinfo Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    I may have misunderstood as the question is hardly clear. I think the OP wants to pass a filename i.e. "myimage" and his PHP script works out from the file content that it's supposed to be "myimage.jpg" ? If so, I don't think there's any inbuilt support for this - there might be classes out there that support common filetypes..
     
    adamsinfo, Jul 19, 2010 IP
  6. c_programmer

    c_programmer Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There is no direct way to do this, most filetypes have their identification somewhere at the beginning. But not all do and they can be easily forged unless you use processor intensive checking.
     
    c_programmer, Jul 19, 2010 IP