Jump to content

how to get the name of the file including a file from the included file


efficacious

Recommended Posts

how to get the name of the file including a file from the included file,

 

This one has me mixed up a bit.. I am trying to record site activity information from a common.php file using a user object. But since the file is included into different php files based on different situations I need a dynamic way of finding the file name that is including it. I could be over complicating things but right now this seems like the best solution other wise I'll have to rewrite the code on every page i write. Is there a function for doing this? Or if someone gets what I'm trying to do if they could point me to the direction of some more information on it.

 

Thanks.

Link to comment
Share on other sites

nevermind i figured it out thanks neways all. 

 

If anyones curious

//Get the File name
$LongName = __FILE__;
$SplitName = explode("/", $LongName);
$CurrFile = $SplitName[count($SplitName) - 1];
echo($CurrFile);

 

Almost figured it out.. the __FILE__ constant wasn't updating each page change I'm not sure why but this seems to have fixed it.

 

Replaced:

$LongName = __FILE__;

 

With:

$LongName = $_SERVER["SCRIPT_NAME"];

 

Link to comment
Share on other sites

__FILE__ will always return the name of the file it's residing in.

 

It says in the manual - The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

Link to comment
Share on other sites

__FILE__ will always return the name of the file it's residing in.

 

It says in the manual - The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

 

ty you for the explanation xyph

Protip: basename.

 

doesn't that require me to already know the path of the file?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.