Use PHP to get the current page/file name

I probably already have this posted somewhere, I know I use it a ton but always have to look it up for the exact syntax. Here’s how you can get the name of the current file (ie. if your file is “aboutus.php” this will echo “aboutus.php”)
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode(’/', $currentFile);
echo $parts[count($parts) - 1];
?>

I’ve seen variations on it, as I said, I always have to look it up for the exact syntax - but today I found that from here. Also here - which seems to list a bunch of other useful snippets.

12 Responses to “Use PHP to get the current page/file name”

  1. gafeman Says:

    hi Jennifer,
    also you can do it with:

    basename($_SERVER['PHP_SELF'])

    and

    basename(__FILE__)

    Greetings from Spain :)

  2. Walid Says:

    echo $parts[count($parts) - 1];
    is the same as
    echo end($parts)

  3. aequalsb Says:

    actually…

    “echo basename( $_SERVER['PHP_SELF'] )” and “echo basename( __FILE__ )” can return the same results or entirely different results.

    why? __FILE__ always refers to the file ITSELF — whether it is included or not whereas PHP_SELF always refers to the “parent” file (the one doing the including).

    you can memorize it like this –> (P)HP_SELF = (P)arent and __FILE__ = __child

    for more details, check this URL where i’ve set up an example:
    http://abdstudios.net/examples/dirname-basename/

    i hope my studies to understand this can help everyone else :)

  4. Farhan Razzak Says:

    Good example Mr.aequalsb
    u have clearly explain both things ,
    but $_SERVER["PHP-SELF"] gives the absolute path not the exact file name.

  5. meowminx Says:

    Hi! I wanted to leave this comment in the htaccess bit, but I couldn’t find the comment form. I just want to thank you for the piece of code in the htaccess you have! I finally got it working without throwing a 500 internal server error! My friend has been puzzled as to why the code she gave me kept doing that! Thank you so much!

  6. aequalsb Says:

    @Farhan

    that’s why you use the basename function (as clearly inidcated in the example)
    basename( $_SERVER['PHP_SELF'] )

    so “echo $_SERVER['PHP_SELF'] will output something like
    /www/htdocs/index.php
    whereas “echo basename ( $_SERVER['PHP_SELF'] ) will output
    index.php

    basename() is a built-in PHP function…

  7. _ck_ Says:

    If you dig around the wordpress/bbpress code you’ll see that PHP_SELF doesn’t always work as wanted on Windows servers with PHP, etc. They have a function that tries it three different ways!

  8. bahodir Says:

    filename without path can be found by this way:

    substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"],”/”) + 1)

  9. aequalsb Says:

    my god…

    just use –>
    basename( $_SERVER[”SCRIPT_NAME”] )

  10. Brad Says:

    How would you go about tweaking this to make it show “included” php flie names?

  11. aequalsb Says:

    @Brad

    if i’m understanding you correctly, you will have to track this yourself through some scripting method.

    example:
    ========================

    ========================

    unfortunately, there is no built-in function to automatically track a set of files that were included — that i know of…

  12. aequalsb Says:

    oops… in the post above, the PHP code wasn’t translated automatically into HTML entities and looks like it may have been removed…

    maybe scriptygoddess can check the post and fix it. otherwise, i no longer have the time to resubmit my post…

Leave a Reply

Subscribe without commenting