Get filename from url in php

Hello Friends. To get filename from the url use this function.

 function get_filename(){
    $php_self = $_SERVER['PHP_SELF'];
    $filename = explode("/", $php_self);
    $filename = array_reverse($filename);
    return $filename[0];
 }


Call function like this

echo get_filename();
Get filename from url in php Get filename from url in php Reviewed by JS Pixels on April 25, 2011 Rating: 5

2 comments:

  1. Cool, but it’s not working if you have some parameters in the URL!!

    I tried this:

    $filename = basename($url);
    $parts=explode("?",$filename);
    $filename = $parts[0];
    echo $filename;

    And it works!!

    ReplyDelete
  2. Thanks for your comments !!
    But that code also works. Please try again..

    ReplyDelete

Altaf Web. Powered by Blogger.