Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the current filename as a variable

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi

A quickie if I may :)

How do I retrieve the current filename in a php script?

I'd like something along the lines of:

$lstrCurrentFile = <function to retrieve the name of the .php file that this line of code is in>

Thanks
-Colin
 
The variable $_SERVER['PHP_SELF'] contains the URI to the PHP script that is being executed. //Daniel
 
Thanks for that.

I tried

echo $_SERVER['PHP_SELF'];

in a .php file but when run through the browser (IE5) while the rest of the php appears nothing in the above echo does.

Any thoughts?

Thanks
-Colin
 
depends on version, try

echo $PHP_SELF; ***************************************
Party on, dudes!
[cannon]
 
Hi again

Thanks for the advice earlier - with a twist of lemon its shown me the answer I needed.

As mentioned above, echo $_SERVER['PHP_SELF']; returned blank. Looking at the output from phpinfo() I see that the PHP_SELF variable is blank (anyone know why?).

However the variable SCRIPT_NAME contains the URI I need instead.

Ta!
-Colin

 
to check the difference between the old way and the new way:

echo &quot;phpself old :$PHP_SELF&quot;;
echo &quot;<br>&quot;;
echo &quot;phpself new: $_SERVER['PHP_SELF']&quot;; ***************************************
Party on, dudes!
[cannon]
 
Apache.

On my laptop is PWS with version 4.?? (the latest version)
which means I need to use $_SERVER[PHP_SELF]

use the code above and see which one returns the value you are looking for. ***************************************
Party on, dudes!
[cannon]
 
Hello all: don't forget that PHP has a coupld of very useful &quot;magic constants&quot;: __FILE__ and __LINE__

Code:
<?php

echo &quot;I am file &quot; . __FILE__;

echo &quot;This is line &quot; . __LINE__;

?>

(now, if there were only a setting so we could use these inside includes or requires, but still reference the main file making the call, instead of the callee... Perl has such a construct. It would make debugging so much easier.)

See and -------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top