0

I am new to php. I am trying to get relative path of my project, but not exactly as required.

I mean I have a directory structure like : '/opt/www/abc/myproj/folder1/folder2/myfile.php'

So in myfile.php I am trying to get path till my project ie., /opt/www/abc/myproj

I have tried differnt ways like using $_SERVER["DOCUMENT_ROOT"], dirname(__FILE__), basename(__FILE__) etc.. but still no luck.

Could anyone please tell me how can I achieve above.

chris85
  • 23,846
  • 7
  • 34
  • 51
John
  • 281
  • 1
  • 4
  • 9

2 Answers2

2

Does dirname(__FILE__) not return anything? It should give you the location of the script from where you are running. You can then traverse up the path using

. DIRECTORY_SEPARATOR . '..'

for each level upwards so for one level above would be:

dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'

Wrap a realpath() function around it to tidy things up... So for your particular scenario:

realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..')
Trung Nguyen
  • 519
  • 3
  • 18
0

you can access the file using,

$path=$_SERVER["DOCUMENT_ROOT"].'/myproj/folder1/folder2/myfile.php';