Newbie trying to get relative paths to work.

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Newbie trying to get relative paths to work.

Postby mulder » 08. August 2006 22:19

I've worked with with PHP a little in the past, but I installed XAMPP to start learning. I'm creating a test site on my localhost first and uploading to a hosted server to implement. I just can't get relative paths to work on one or the other. I think I know the issue, but I'm wondering if there is a way to fix it.

For example, I have a site at http://localhost/site. I kept all the default settings for XAMPP so htdocs is the root folder. Under the site folder, I create the subfolders and files for the website. However, when I try to use relative paths, it goes back up to htdocs instead of site. DOCUMENT_ROOT is set to htdocs in the PHP info and I would like to keep it that way. Is it possible to to set site as the DOCUMENT_ROOT for the folders just under it only so when I upload to a hosted server I can keep all my relative path code?

Here is a specific example:

Under the site folder, I have index.php with this code:
Code: Select all
<?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"); ?>


However, htdocs is set as DR I get this error:
Warning: include(C:/Program Files/xampp/htdocs/inc/menu.php) [function.include]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\site\index.php on line 48


I know this works when uploaded, but I'd like for it to work on the localhost so I can see the final product before publishing it.

Thanks for the help.
mulder
 
Posts: 5
Joined: 08. August 2006 22:03
Location: Birmingham, Alabama, USA

Postby Wiedmann » 08. August 2006 22:30

I just can't get relative paths to work on one or the other.
Code: Select all
<?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"); ?>


Then use relative paths and no absolute...
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby mulder » 09. August 2006 20:45

Hi. Thanks for the reply. I'm a little confused. I'm already using relative paths, or at least I thought so. Do I have something wrong in the code I put up? If so, how would I write it differently?

Thanks.
mulder
 
Posts: 5
Joined: 08. August 2006 22:03
Location: Birmingham, Alabama, USA

Postby Wiedmann » 09. August 2006 20:57

I'm already using relative paths, or at least I thought so

Well, the path you are using in your include statement is:
$_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"

2 questions:
1) Make a:
Code: Select all
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"; ?>

What can you see?

2) What is the definition for a relative or an absolute path?
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby mulder » 09. August 2006 23:08

1) When I create that, I get this output (which I expected based on the problems I was having and how htdocs is considered the root):
Code: Select all
C:/Program Files/xampp/htdocs/inc/menu.php


2) If you're wanting me to definte them, I understand relative paths to be those that access other files relative to the currently accessed file. Under a hosted website where site is the root folder:

site
---inc
---img
---events

When I'm in /site/index.php and I want to link to the events folder I reference it by <a href="/events"> or I can also use <a href="events">. However, the second option won't be dynamic between all the pages on the site, because it would trying to reference as a subfolder of whatever folder I'm in at that moment. So, I stick with <a href="/events">.

This stuff I get. My question deals strictly with getting XAMPP to work with my relative paths without having to edit the code. I want to download my hosted files and view the site locally as it is online.

Code: Select all
<?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"); ?>


That piece of code works fine on the hosted site, because everything is at the root folder. I know that when I am testing things locally that my files aren't at the root. They are at root/site/, where root is the htdocs.

htdocs
---site
------inc
------img
------events

To solve my problem I could just copy all of the stuff inside the site folder to the htdocs. But doing this makes it difficult to keep projects organized.

Is there anyway to set the root for specific folders under htdocs? Does that make sense? Or is there any way to code the files under the site folder to think that site is the root and not htdocs without changing this for all the other projects?

Thanks for helping me out with the newbie questions. I'm still trying to get the hang of this.
mulder
 
Posts: 5
Joined: 08. August 2006 22:03
Location: Birmingham, Alabama, USA

Postby Wiedmann » 09. August 2006 23:38

1) When I create that, I get this output
C:/Program Files/xampp/htdocs/inc/menu.php


2) If you're wanting me to definte them, I understand relative paths to be those that access other files relative to the currently accessed file.

An absolute path starts with a driveletter or a slash "/".

A relative path starts not with a driveletter or a slash "/" and works relative to the current working directory (the directory of the script you access with your browser)
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby mulder » 10. August 2006 15:02

Ah, I understand now. After re-reading my own post aobve, I should have got that. When I was saying that on my "menu.php" needed to have the "/" so that it would go back to the root directory, I was saying I needed the absolute path to that file no matter where I am in the site.

And when I take away the "/" the folders become relative to the current file.

Ok, that makes total sense. Thanks for clearing this up.

Do you know if my second question is possible? For testing locally, is it possible to set up folders as the root for that specific project inside XAMPP?
mulder
 
Posts: 5
Joined: 08. August 2006 22:03
Location: Birmingham, Alabama, USA

Postby Wiedmann » 10. August 2006 15:32

1st)
If you consistently use relative paths in your project, you can move the complete project where ever you want and it work.

Do you know if my second question is possible? For testing locally, is it possible to set up folders as the root for that specific project inside XAMPP?

IMHO you are talking about this?
mulder wrote:When I'm in /site/index.php and I want to link to the events folder I reference it by <a href="/events"> or I can also use <a href="events">. However, the second option won't be dynamic between all the pages on the site, because it would trying to reference as a subfolder of whatever folder I'm in at that moment. So, I stick with <a href="/events">.

Yes. But I don't really understand the problem you have with the second option ...
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby mulder » 10. August 2006 18:40

Maybe it's something I'm doing wrong, but here is the extent of the issues I'm still having.

Here is my directory structure for XAMPP's root:

Code: Select all
c:\program files\xampp\htdocs


I have a website I'm working on inside that, like so:

Code: Select all
c:\program files\xampp\htdocs\mysite
c:\program files\xampp\htdocs\mysite\inc
c:\program files\xampp\htdocs\mysite\img
c:\program files\xampp\htdocs\mysite\events


I create an index.php inside the mysite directory. This allows me to access it via http://localhost/mysite. Here is some example code from that index.php file:

Code: Select all
<link rel="stylesheet" type="text/css" href="/screen.css" />
...
      <div id="sidebar">
         <?php include("inc/menu.php"); ?>
         <a class="hide" href="#top" accesskey="1">Top of page</a>

         <?php include("inc/whatsnew.php"); ?>

      </div>
      <div class="clear">&nbsp;</div>
   </div>

   <div id="footer"><?php include("inc/footer.php"); ?></div>


Before our discussion, I had the PHP Include statements as <?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/menu.php"); ?>.

Here is some code of the menu.php file:

Code: Select all
<h2 class="sidelink menuheader"><a id="sitemenu"></a>Site menu:</h2>
<a class="sidelink" href="/meetings">Meetings</a>
<span class="hide"> | </span>
<a class="sidelink" href="/bylaws">Bylaws</a>
<span class="hide"> | </span>
<a class="sidelink" href="/members" title="This area is password protected">Members</a>
<span class="hide"> | </span>
<a class="sidelink" href="/contactus">Contact Us</a>
<span class="hide"> | </span>


There are multiple issues that I am having trouble getting around. First is the PHP Includes. They work fine for this particular mysite\index.php file, because the reference to the inc folder is relative. However, once I create a php file within a subdirectory, say mysite\meetings\index.php, those include statements stop working. Rightly so, because they aren't relative to the meetings directory. So I code in the absolute paths for mysite\meetings\index.php like so:

Code: Select all
<?php include("/inc/whatsnew.php"); ?>


But that doesn't work, because it thinks the absolute path begins at htdocs and spits it out as htdocs\inc\whatsnew.php, but I need it to do htdocs\mysite\inc\whatsnew.php.

My next issues are with the menu.php file. It has to include absolute paths, because it's being referenced all over the site. Once again, I get the same issue, because since I am using the "/" for the absolute path, it goes back to htdocs before looking for the inc folder, when I want it going to mysite.

Do you see how I cannot make paths correctly using this? When I am under the htdocs\mysite directory, I want all the "/" absolute paths to start at mysite, not htdocs.

That's why I was asking if there is a way to set the root folder as mysite\ for only the files under mysite.

I really do appreciate your help. If there is something I am doing wrong or not getting or not explaining well enough, please let me know. Thanks.
mulder
 
Posts: 5
Joined: 08. August 2006 22:03
Location: Birmingham, Alabama, USA

Postby Izzy » 11. August 2006 03:53

Relative Paths
Relative paths are paths the are defined from the location of the current page.

Some rules of creating a relative path:
links in the same directory as the page have no path information listed: mysite
sub-directories are listed without any preceding slashes: downloads/mysite
links up one directory are listed as: ../mysite

Relative paths are sometimes more difficult to work with as the path to a web element will change depending on the location of the current page.

You htdocs directory is your web root / or \ directory.

Try this site for some informative Relative Path reading:
http://www.webteacher.org/winexp/navigate/path.html
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 92 guests