Detecting extension

Discussion in 'PHP' started by hip_hop_x, Nov 24, 2007.

  1. #1
    If anyone has the experience, how could I obtain the image extension, and if it's swf to do something else? Remote image, and I'm using php4.
    One more thing, the link is stored in a database.
    if I'll do a preg match, there will be a risk, that the image to contain 2 or more times 2 different words, giving an error.

    example(doesn't work) h ttp://upload.wikimedia.org/imagif/page.swf
     
    hip_hop_x, Nov 24, 2007 IP
  2. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    nvm, I figured out how to.
     
    hip_hop_x, Nov 24, 2007 IP
  3. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In case anyone else is wondering or you want something succinct.

    You can use the pathinfo function to accomplish this.
     
    tonybogs, Nov 24, 2007 IP
  4. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #4
    post ur solution to make this thread more beneficial for other or just ask the MOD to delete it ...
     
    commandos, Nov 24, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    
    
    $name = $database['filename'];
    $extension = end(explode('.',$name));
    
    
    PHP:
    Peace,
     
    Barti1987, Nov 24, 2007 IP
  6. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #6
    
    //$pimp[8] is the query
    $pro=strrchr($pimp[8],'.');
    if($pro == ".swf")
    { //flash 
    } 
    else{//image 
    }
    PHP:
     
    hip_hop_x, Nov 25, 2007 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Your code will not work if the URL contains another "." in it.

    Peace,
     
    Barti1987, Nov 25, 2007 IP
  8. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #8
    oh, you're right azizny, thanks for helping me.
     
    hip_hop_x, Nov 25, 2007 IP
  9. EntertainmentScripts

    EntertainmentScripts Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Or just create a function:

    
    function get_file_extension($file)
    {
        $ext = explode(".", $file);
        $extcount = count($ext);
        return strtolower($ext[$extcount - 1]);
    }
    
    PHP:
    
    
    $file = "name.swf";
    $extension = get_file_extension($file);
    
    if ( $extension == "swf" )
    {
    // Extension is SWF so execute code
    }
    
    PHP:
     
    EntertainmentScripts, Nov 25, 2007 IP