NOTE: wherever you see /localhost/ just imagine there is a “http:/” before it. This is because this forum doesn’t allow you to enter a URL (even though strictly speaking it’s not a URL you can access anyway) until you make 10 forum posts.
I’m building a PHP based website on my local machine and because I have other sites under my localhost I have to put them into folders (e.g. /localhost/SITENAME/).
I want to use absolute paths so I can move my PHP web pages into sub folders (e.g. /localhost/SITENAME/PRODUCT-NAME/).
The problem is that I have to manually type into each and every file path “/SITENAME/” at the start, because if I use just a forward slash / then it will try to find the file here: “/localhost/”.
I’ve tried using
but that doesn’t work as expected?
I want to be able to use something like a PHP SERVER variable that can find the root of the website folder and so when I upload the site to the live server I don’t have to change any code because the PHP variable will detect the new server root.
Some of the $_SERVER variables show C:\ rather than /SITENAME/ and others such as $_SERVER[‘SCRIPT_FILENAME’] or $_SERVER[‘PHP_SELF’] show both the sub folder and the current file name (e.g. /localhost/SITENAME/PRODUCT-NAME/index.php) and it seems like it would be a server resource hog to have to call a function which explodes the string into an Array and then just use the relevant part of the URL that I need, but also that it might not work then as expected when putting the site on the live server and so again I have to change the original code which is something I want to avoid.
Any help greatly appreciated as I’m sure there is a fairly simply solution.
Hi logic_earth, thanks for posting but this hasn’t worked.
If you take a look at the following example…
echo "<a href='$root'>click here to go to home page</a>";
Produces a link that tries to direct to the path…
…which doesn’t work when you click on the link.
Ideally what I want it to do is to pick up /SITENAME rather than all the c:\ drive information.
Any suggestions? As per my original post I don’t want to have to run a function on every page that explodes the variable into an Array as that code would then break when the site is put on the live server (as it would no longer be in a folder called “SITENAME”).
You are confusing your terms. Document root is a term that corresponds to the physical location of the files on the server. It doesn’t have anything to do with the URL that your site is accessed at. Both $_SERVER[ ‘DOCUMENT_ROOT’ ] and logic’s method will help you find that.
You are looking for the main folder that this site is accessed at. I can’t think of a way for PHP to know that without changing code in a config file, if you’re going to have the site at /SITENAME on localhost and just at the document root in production.
As per what Jerrac said I did think to use a include file but it soon became apparent that it wouldn’t work that well at all.
Tomorrow I’ll check out Fristi’s option as I’m not using Apache but a Windows server so the issue that was raised by Stormrider should hopefully not affect my use case. If it works I’ll post my code here in case it helps any one else.
Thanks again for everyone’s post, I’ll let you know tomorrow how far I get or whether it’s back to the drawing board.
Maybe I’m just being blind but why can’t you just write the base path to somewhere readily available in your script (a config file, or whatever) and use that value wherever it is needed? Depending on your file structure that might be an easy option.
is a IIS windows server root address.
Are you using IIS as your dev server or Apache, if you are using IIS this could explain why none of the fixes offered by others here are working.
The advice most here are offering you is based on the assumption that you are running apache, not IIS
Ummm they would work with IIS just fine. Nothing posted here is server-software-dependent.
PHP on IIS or Apache is not some vastly different language.
If one was following an MVC approach to a web application, or any approach that would funnel every request to a single file (mostly index.php). Then all your problems dealing with paths would come to an end. Second during development building every site or application as it were real is key. By that I mean giving it a real domain and isolated path. (No localhost/site-or-app-name)