getting access to the name of the file the code is in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

getting access to the name of the file the code is in

Post 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
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

The file constant is called __FILE__
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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
Post Reply