homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Correct file path for php includes: relative, absolute, other?
ewwatson




msg:3610647
 1:00 am on Mar 26, 2008 (gmt 0)

Hello, I have 3 folders including my root folder - they are: root folder, content folder, and includes folder. The links in my nav and footer php include look like ../index.php. This link works for all the files in the content folder but not the root folder. And if I make the link like index.php it works for the opposite and not for the others.

How should I be pointing my links to my files within my php includes? How do you guys do it? I have found some answers but they all seem a little unnecessary - they are all these small to large php scripts. Well there has to be some way to just point at file, regardless of where it sits, and be done with it, right? Thanks again!

 

g1smd




msg:3610692
 2:20 am on Mar 26, 2008 (gmt 0)

Think it through like this. The include file hasn't got a URL of it's own. It only has a folder location on a hard drive.

The content of the include file will be included internally by the server, to be a part of some other page, at the URL for that other page. Only then will the browser be able to resolve a URL from that data in the link.

Use URLs that being with / to show the full path tto the other content. It will then always resolve correctly. URLs in links are otherwise resolved by the browser using the URL for the currently viewed page as a base.

Marcia




msg:3610697
 2:27 am on Mar 26, 2008 (gmt 0)

For using includes on pages in /directories/ the easiest way when includes are in their own directory called /includes/ is to call the includes for the pages like this:

<?php include("../includes/thecode.php"); ?>

For pages in the root, this is what I use that works:

<?php include ("includes/thecode.php"); ?>

For using absolute paths I believe you'd have to use the actual Apache paths (not the same as the URI). It's easier to just use relative links, but they can't be done the same way for pages in the root as they are for pages in /directories/.

g1smd




msg:3610937
 11:37 am on Mar 26, 2008 (gmt 0)

Typo:

Use URLs that being with /

should be:

Use URLs that begin with /

PHP_Chimp




msg:3610943
 11:50 am on Mar 26, 2008 (gmt 0)

If you use absolute url's for includes then you need to use the file system path, not the web path. As the include is carried out by the server not the browser.

Example -

include('/var/www/domains/example.com/htdocs/include/some_file.php');

Will include the same file from any location, but this is the file system location. As the web root, /, would be
'/var/www/domains/example.com/htdocs/'
So if you try -

include('/include/some_file.php');

You will probably get an error and if you dont get an error then you are unlucky as you are not including the same file as you thought.

On some of the sites where I have subdomains set up I generally use absolute url's for includes. As the subdomains sit on the same server, however have different file system paths to the web root. So instead of copying the code into all of the subdomains I can use 1 set of code across all of them. This does rely on settings in php.ini that are not always available on all servers, so this may or may not work for you anyway.

<edit>
Same thing as Marcia said above.

[edited by: PHP_Chimp at 11:51 am (utc) on Mar. 26, 2008]

g1smd




msg:3610947
 11:53 am on Mar 26, 2008 (gmt 0)

I sometimes do set the include path using PHP_Value in the .htaccess file.

coopster




msg:3611003
 1:18 pm on Mar 26, 2008 (gmt 0)

Another option from our PHP Forum Library [webmasterworld.com] ...
Control your included files [webmasterworld.com]

ewwatson




msg:3611423
 7:00 pm on Mar 26, 2008 (gmt 0)

Alright, thanks for all the extremely helpful information guys. That should set me strait. Take care!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved