Page 1 of 1
getting access to the name of the file the code is in
Posted: Mon Sep 26, 2005 2:15 am
by jasongr
Hello
Is there a way to get access to the full path for the file my code is in?
For example, if I have 2 files:
c:\a.php
c:\innerFolder\included.php
page a.php includes file included.php like so:
require_once('c:/innerFolder/included.php');
now if I am in included.php, I need a code that will give me its full path: 'c:/innerFolder/included.php'
if I am in a.php, I need the file path: 'c:/a.php'
I need to write a code that outputs the name of the file and I will put it at the top of all my files in the project
I need to use it to know through what files my script is passing
thanks
Posted: Mon Sep 26, 2005 6:42 am
by pilau
The file constant is called __FILE__
Posted: Mon Sep 26, 2005 6:45 am
by jasongr
Important: I need an automatic solution as I even though my example has 2 files, my actual code has thousands of files so it is not practical to modify each and every one of them. I need some how to make sure that the following code is injected at the beginning of EACH of my .php files in the project:
Code: Select all
if (!isset($fileNameHandle)) {
$fileNameHandle = fopen($_SERVER['DOCUMENT_ROOT'] . '/logs/' . files.txt', "at+");
}
// get path to the file here
fwrite($fileNameHandle, __FILE__ . "\n");
I don't want to take this code and to copy-paste it at the beginning of EVERY file
I need a solution that will take that code and will implicitly inject it in runtime to every .php file
I have thousands of files and I cannot litter them with such code which should be defiend only once in a single place in the application
I need a solution that doesn modify ANY of my files
Mabye a solution that automatically injects this code implicitly by apache or by modifying php.ini
Posted: Mon Sep 26, 2005 6:52 am
by Chris Corbyn
Write a PHP script to loop recursively over all dirs and if it finds a PHP file, fopen() it, fwrite() to it and fclose() it.... not as simple as it sounds to do it recursively

Posted: Mon Sep 26, 2005 6:53 am
by jasongr
This is not good
As I mentioned, I need a solution that doesn't replicate my code in all the files
I don't want to modify my files
Posted: Mon Sep 26, 2005 6:55 am
by Jenk
Posted: Mon Sep 26, 2005 7:00 am
by jasongr
Thanks Jenk
Your tip was very helpful
I can just add the following snippet at the bottom of every file I want to test:
Code: Select all
$includedFiles = get_included_files();
$fileNameHandle = fopen($_SERVER['DOCUMENT_ROOT'] . '/logs/' . 'files.txt', "at+");
foreach ($includedFiles as $filename) {
fwrite($fileNameHandle, $filename . "\n");
}
fclose($fileNameHandle);
This doesn't polute unneeded files