0

so say my php file "example.php" is in the directory "/user/example"

I thought that the line "$dir = getcwd();" would save "/user/example" to $dir but instead it is saving the directory that the user running the script is in, e.g. if I run the script from the root directory, the $dir variable will contain the root directory.

Is there a command I can use or a tip you can give me so that I can save the directory that the php file is in?

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Eujinks
  • 390
  • 1
  • 16

2 Answers2

0

You can get the path to the current file using the magic constant

__FILE__

And the directory it resides in using:

__DIR__

Sample demo usage:

 <?php
 $dir = __DIR__; // /usr/example
 $filename = __FILE__; // /usr/example/example.php
 $basename = basename(__FILE__); // example.php

Be warned that this is not the file that is executed, but the current script, so if you make an include, or call a method/function from another file these magic constants will change(they're named "constants" solely because you cannot change them yourself).

scragar
  • 6,764
  • 28
  • 36
0

You need to combine your existing code using dirname() with a call to basename():

$parent = basename(dirname($_SERVER['PHP_SELF']));
Otto
  • 343
  • 5
  • 14