Code Snippet

Home » Code Snippets » PHP » Get Current File Name

Get Current File Name

<?php
    $pageName = basename($_SERVER[PHP_SELF]);
?>

Potential use:

<body id="body_<?php echo $pageName; ?>">

Append ID to body to do different CSS styles on different pages.

Subscribe to The Thread

  1. Chris your snippets section is working. I found this on Google it was just what I needed.

  2. Zlatan Halilovic

    sweet

  3. yeah! this is working, thank you.

  4. echo $pageName = basename($_SERVER['SCRIPT_NAME']);

    It will give current file name… as index.php or about.php

  5. But you will have ‘.php’ as id or class. If you want get only name befor the ‘.php’ here is de code:

    // Recuperando o nome do arquivo sem '.php'
      $currentFile = $_SERVER["PHP_SELF"];
      $parts = Explode('/', $currentFile);
      $parts = $parts[count($parts) - 1];
      $parts = Explode('.', $parts);
      $pageName = $parts[0];
    

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~