tags:

views:

38

answers:

3

I can get the currently executing file with the __FILE__ magic PHP constant, but this gives me /var/www/vhosts/.../httpdocs/stacktrace.php. I am trying to get the name of the file so I can pass it into a hidden form field, and then run that script again after the form is submitted.

The script whose file name I am trying to get could be named anything, it's designed to be copied and renamed to any part of a site.

The script will always be included, it'll never be 'the' executed script.

I need to be able to find the path to the file, as it would be used in an include directive (i.e. relative to the include path).

I had hoped using ini_get('include_path') would help me (if it was /var/www/vhosts/.../httpdocs/ then I could just cut that part out of the script's file path and it would work), but that just gave me .:.:.:. I don't even know what that means.

Any push in the right direction would help tonnes.

A: 

Perhaps you are looking for basename() ?

meder
this just gives me the name of the file, and excludes the directories leading up to it that are relative to the include path.
Carson Myers
Ah, I misread. excuse me.
meder
Sorry if I keep misreading as it is late in the night, maybe `dirname()` is what you want?
meder
And you can keep on using dirname, eg `dirname(dirname($var));`
meder
it seems like this will just give you the full path minus the file name, and using it multiple times will give you one less top directory... I'm trying to turn `/var/www/vhosts/.../httpdocs/some/path/to/file.php` into `some/path/to/file.php`
Carson Myers
actually, I found something, I'll post it as an answer
Carson Myers
nevermind, it was already posted.
Carson Myers
+1  A: 

You can use one of the following values to do what you want:

$_SERVER['DOCUMENT_ROOT']
$_SERVER['SCRIPT_FILENAME']

The first returns the docroot, and the second returns the path to 'the' executed script. You can then use this information to get the relative path to the current file.

reko_t
+1  A: 

Not sure if I understood exactly what you want, but how about:

 $root = $_SERVER['DOCUMENT_ROOT'];
 $pathWithLeadingSlash =  substr(__FILE__, strlen($root)); 
 // or
 $pathWithoutLeadingSlash = substr(__FILE__, strlen($root) + 1);
Mads Mobæk