2

I have a website in my local computer. I am using wamp server to run my website.

URL for website is http://localhost/mywebsite/

I put a PHP file in my website inside folder mywebsite/component/add/files/add.php

So the actual path of file is c:\\wamp\www\mywebsite\component\add\files\add.php.

I want to show path c:\wamp\www\mywebsite in my file.

How can I get it? And in future when I move my site to somewhere else, I don't need to change path manually. It must be update automatically. Is there any function for this?

e.g. If I move my site in mywebsite/demo, the path will be C:\wamp\www\mywebsite\demo

I hope this is clear to understand. I don't want to show http://localhost/mywebsite path there.

Can anyone help me ???

PenguinCoder
  • 4,335
  • 1
  • 26
  • 37
Jinal Dabhi
  • 181
  • 1
  • 2
  • 13
  • In your situation, I would create a variable that is accessible on every page (via an include) that contains a prefix and then prefix this to your urls, i.e. `$Prefix = './mywebsite';` and then `View our demo`. When you move your site, you would simply change the `$Prefix` to something like `./newwebsite'`, all of your links would automatically update. – Gavin May 24 '12 at 13:06

3 Answers3

2

The $_SERVER array should have the information you're looking for. For example:

$_SERVER['DOCUMENT_ROOT']

This would be the configured document root for the PHP server. In your case, I believe that would be c:\wamp\www. You can also use:

$_SERVER['SCRIPT_FILENAME']

This would be the absolute path of the current running script. There are also some magic constants that contain useful information, particularly if you also have PHP scripts running outside of a web server context. (Background tasks, etc.)

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Wouldn't this show c:\wamp\www instead, as mywebsite is a sub-folder of www, which is the main application folder? – Gavin May 24 '12 at 13:02
  • @Gavin: Good point, I didn't catch that. Answer updated. Looks like Rinzler had the right idea, too. – David May 24 '12 at 13:09
1

Use:

  $_SERVER['DOCUMENT_ROOT']; 

or

 Perhaps you want: dirname(__FILE__)

http://php.net/manual/en/language.constants.predefined.php

Rinzler
  • 2,139
  • 1
  • 27
  • 44
0

You can use this code

str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME'])

or alternatively

str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(__FILE__));
str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__); // works only in php > 5.3
Fabio
  • 18,856
  • 9
  • 82
  • 114