0

I did this:

echo dirname(____FILE____) 

and it keeps showing as full absolute path as

C:/Program Files (x86)/VertrigoServ/www/dir/file.php

Why is that? I expect it to show

dir/file.php

I am using WAMP which is Apache on Windows.

BTW, calling $_SERVER['DOCUMENT_ROOT'] does the same thing - it shows full path.

John Conde
  • 217,595
  • 99
  • 455
  • 496
netrox
  • 5,224
  • 14
  • 46
  • 60
  • 1
    PHP doc says: "*The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances*". Why would you expect anything else than the file path? – Tchoupi Feb 12 '13 at 00:27
  • Are you trying to obtain relative path of script in webroot? – Miro Hudak Feb 12 '13 at 00:28
  • yes the path relative to root. – netrox Feb 12 '13 at 00:30
  • Duplicate question: http://stackoverflow.com/questions/9337731/dirname-file-on-localhost Thanks. – netrox Feb 12 '13 at 00:47

2 Answers2

1

Try:

$webPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', __FILE__));

It'll take the absolute path to the file and remove the document root portion of it which should leave you with the path and file absolute from the document root.

HamZa
  • 14,671
  • 11
  • 54
  • 75
drew010
  • 68,777
  • 11
  • 134
  • 162
  • `$_SERVER['DOCUMENT_ROOT']` returns a location with / and `__FILE__` returns it with \, thus nothing is replaced at all (tested under windows) – HamZa Feb 12 '13 at 00:39
  • 1
    You could do `str_replace('/', DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT'])` on the doc root to counteract this issue on windows. – drew010 Feb 12 '13 at 00:48
0

Although you don't state, if you're using wordpress I solved this issue by using:

file(bloginfo('home_url').'<path-to-file-without-leading-slash>')
Lee
  • 4,187
  • 6
  • 25
  • 71