How to get the current file name

Asked

Viewed 11,978 times

13

I recently had a problem identifying the current page.

Was using __FILE__ to get the current file, and used substr() to retrieve the string after the last occurrence bar, it turns out that in some operating systems these bars work differently and end up not returning the name in the expected way.

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

What I currently wear:

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

Used to use:

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

I just learned:

 echo basename($_SERVER['PHP_SELF'],'.php');
  • 1

    If it worked, mark my answer as correct. Thank you.

5 answers

14


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 working on IDEONE.

4

Just a little observation: There are two ways to get the file name.

Ex. 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'); ?>

When "compiling" the B.php file, the final 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 being executed.

;)

1

You can use the DIRECTORY_SEPARATOR constant to correctly detect which bar is used by your operating system for directory separation.

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

The code below is multi-platform:

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

Reference: http://php.net/manual/en/dir.constants.php

  • substr(strrchr(FILE, DIRECTORY_SEPARATOR),1,-4); would solve the problem, could it say whether DIRECTORY_SEPARATOR is "compatible" with different S.O by using the bars?

  • This answer may help you http://answall.com/questions/2304/diferen%C3%A7a-entre-path-separator-e-directory-separator

  • Yes, DIRECTORY_SEPARATOR is a constant that stores the correct operating system "bar". For knowledge only, there is also PATH_SEPARATOR (separator for different so’s system paths) and PHP_EOL (stores the line break of your OS). http://php.net/manual/en/dir.constants.php

  • It would be interesting for you to edit your question by putting this information.

  • Change made. You can already mark as correct :p

  • Hi Willian, I suggest a read here.

Show 1 more comment

1

The combination explode() & end() can get the same result(take the current file name);

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

<?php
  $arquivo = explode(DIRECTORY_SEPARATOR, __FILE__);
  echo end($arquivo);
  • Using arrays instead of text manipulation in this situation would be faster?

  • @Williamokano, I can’t tell, can test on https://3v4l.org/ I don’t have access now to check the performace.

  • @Williamokano yours is a few milliseconds faster than the rray solution, but the solution "basename($_SERVER['PHP_SELF'],'.php')" is 20% faster than yours;

  • @Gabrielrodrigues, post the link :)

  • @Gabrielrodrigues, only basename(__FILE__) doesn’t solve?

  • @rray I just want the name. with this comes the extension too, whatever use _FILE or PHP_SELF, I benchmark on my machine using the microtime_float function I found in the documentation: http://php.net/manual/en/function.microtime.php

Show 1 more comment

0

I transform the string constant FILE in 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 single path (path) to the file

<?php

    // caminho para o arquivo
    $caminho = realpath(dirname(__FILE__));
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.