Author Topic: [SOLVED] Get filename of script currently being run..?  (Read 245 times)

0 Members and 1 Guest are viewing this topic.

Offline DamienRocheTopic starter

  • Enthusiast
  • Posts: 282
    • View Profile
[SOLVED] Get filename of script currently being run..?
« on: September 30, 2008, 01:51:29 PM »
Is there a command for obtaining the name of the script being run..obviously a command I would have to run in the script?

Can I do this:

$thisfile = ///// command I want

file_get_contents($thisfile);

Basically I want to run my test scripts but also feature the actual code on the same page.

Thanks for any help.
Before I ask a question, I: search google, search php.net, experiment etc. If I ask a question here, I really do need help. Thank you to everybody who has helped me!

Offline discomatt

  • Addict
  • Posts: 1,936
  • Gender: Male
    • View Profile
Re: Get filename of script currently being run..?
« Reply #1 on: September 30, 2008, 01:54:29 PM »
Code: [Select]
$contents = file_get_contents( __FILE__ );

Offline discomatt

  • Addict
  • Posts: 1,936
  • Gender: Male
    • View Profile
Re: Get filename of script currently being run..?
« Reply #2 on: September 30, 2008, 01:57:17 PM »
This will give funny results on included files. For this you may want

Code: [Select]
file_get_contents( $_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'] );
I'm 99% sure DOCUMENT_ROOT is derived from the server... but I'm not entirely sure about SCRIPT_NAME... so you may want to sanitize.

Offline DamienRocheTopic starter

  • Enthusiast
  • Posts: 282
    • View Profile
Re: Get filename of script currently being run..?
« Reply #3 on: September 30, 2008, 02:03:47 PM »
Thanks discomatt! __FILE__ works perfectly. I'm running it on a local machine so not sure how that will effect the second method.

I'll give the other method a try if I decide to put this online.

Thanks again!
« Last Edit: September 30, 2008, 02:04:24 PM by DamienRoche »
Before I ask a question, I: search google, search php.net, experiment etc. If I ask a question here, I really do need help. Thank you to everybody who has helped me!

Offline discomatt

  • Addict
  • Posts: 1,936
  • Gender: Male
    • View Profile
Re: [SOLVED] Get filename of script currently being run..?
« Reply #4 on: September 30, 2008, 02:06:06 PM »
This should also work, assuming the var is defined

Code: [Select]
$contents = file_get_contents( $_SERVER['SCRIPT_FILENAME'] );