efficacious Posted May 31, 2011 Share Posted May 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/ Share on other sites More sharing options...
efficacious Posted May 31, 2011 Author Share Posted May 31, 2011 nvm 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); Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223242 Share on other sites More sharing options...
efficacious Posted June 1, 2011 Author Share Posted June 1, 2011 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223252 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 __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. Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223254 Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 Protip: basename. Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223258 Share on other sites More sharing options...
efficacious Posted June 1, 2011 Author Share Posted June 1, 2011 __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? Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223278 Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 Yeah. I was commenting on the rest of the code. Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223298 Share on other sites More sharing options...
efficacious Posted June 1, 2011 Author Share Posted June 1, 2011 Yeah. I was commenting on the rest of the code. ah i gotcha. Quote Link to comment https://forums.phpfreaks.com/topic/238040-how-to-get-the-name-of-the-file-including-a-file-from-the-included-file/#findComment-1223339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.