How to get the name of the current file

10

Recently I had a problem identifying the current page.

I was using __FILE__ to get the current file, and used substr() to get the string after the last occurrence of bars, it turns out that on some operating systems these bars work differently and end up not returning the name as expected.

Is there any function / variable that already returns the file name?

What I currently use:

 echo substr(__FILE__, strrpos(__FILE__, '\') + 1, -4);

Used before:

 echo substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/') + 1, -4);

I learned now:

 echo basename($_SERVER['PHP_SELF'],'.php');
    
asked by anonymous 23.11.2015 / 19:02

5 answers

10

I think the simplest thing is this:

<?php
   basename( __FILE__ );
?>

Alternative, which gives even more information:

<?php
   $path_parts = pathinfo( __FILE__ );
   echo $path_parts['basename'];
?>

See both running on IDEONE .

    
23.11.2015 / 19:51
1

The explode() & end() can get the same result (get the current filename);

The explode() transforms the string returned by __FILE__ into an array, the delimit is the directory bar that can be \ or / and end() returns the last element of the array that is the name of the file.

<?php
  $arquivo = explode(DIRECTORY_SEPARATOR, __FILE__);
  echo end($arquivo);
    
23.11.2015 / 19:23
1

You can use the DIRECTORY_SEPARATOR constant to properly detect which bar is used by your operating system to separate directories.

As a matter of curiosity, there are other constants, such as PATH_SEPARATOR (which stores the character responsible for separating directories from path - in windows is'; 'and' linux ':') and PHP_EOL, which stores the line break of your operating system.

The code below is cross-platform:

<?php
$file =  substr(strrchr(__FILE__, DIRECTORY_SEPARATOR), 1);
echo $file;

Reference: link

    
23.11.2015 / 19:11
1

Only one observation: There are two ways to get the file name.

Eg: A.php file

<?php
   $dir1 = basename($_SERVER['PHP_SELF']);
   $dir2 = basename( __FILE__ );
        echo "<br><br>Dir 1: ".$dir1;
        echo "<br><br>Dir 2: ".$dir2;
?>

B.php file

<? php inlude('A.php'); ?>

By "compiling" the B.php file, the end result will be:
Dir 1: B.php
Dir 2: A.php

Conclusion: One prints the location of the file being imported and the other prints the file that is being executed.

;)

    
02.11.2016 / 21:31
0

I transform the constant string FILE into an array using the directory separator (according to operating system - DIRECTORY_SEPARATOR) as the field separator to create the array. The last element of this array is the file name and its extension.

Examples:

<?php

    $foo = explode(DIRECTORY_SEPARATOR, __FILE__);

    // o ultimo elemento deste array é o nome do arquivo
    $arquivo = end($foo);
?>

The same reasoning can be used to get the file extension

<?php

    $foo = explode('.', __FILE__);

    // o último elemento do arranjo
    $extensao = end($foo);
?>

Extra: getting the path to the file

<?php

    // caminho para o arquivo
    $caminho = realpath(dirname(__FILE__));
?>
    
15.04.2016 / 08:53