Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-01-2009, 06:33 AM   PM User | #1
ajhauser
Regular Coder

 
ajhauser's Avatar
 
Join Date: Nov 2007
Location: Earlville. It's where Earls come from.
Posts: 224
Thanks: 73
Thanked 1 Time in 1 Post
ajhauser is an unknown quantity at this point
PHP include paths

Hello.
If I were to use a PHP include in my site, such as:

PHP Code:
<!DOCTYPE ...>
<html>
...
<body>
<?php include('/path/to/include.ext');?>
</body>
Can I use an absolute path instead of a relative one? On my site now I use the code:

PHP Code:
<?php include('includes/nav.html'); ?>
Which will be fine until I ove a file up or down a level. I have found several topics on the web about this issue but I am new to PHP and have no friggin idea what they are talking about.

Is an absolute path possible?
Thanks!'-AJ
ajhauser is offline   Reply With Quote
Old 05-01-2009, 08:17 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I think you misunderstand. The first block of code is an absolute path (starts with / for root), and the second is a relative to the current script.

My recommendation is to always use relative where you can. But, be aware that PHP takes its cwd (current working directory) from the executing script, not the included ones.
Code:
/+
 + Users/
 +---+ YourSite
        +-----+ public_html
                 +---- index.php
                 + includes
                 +---- menu.php
                 +---- TemplateManager.php
Now, if you include menu.php into index.php using include 'includes/menu.php'; and the same with templateManager, then use templateManager in menu.php like so: include 'TemplateManager.php';, this will fail. This is because the cwd of the index.php is public_html, while the menu cannot find the TemplateManager.php. Solve by creating a relative absolute path (sorry, I don't have a better name for it). This lets you attach to any file relative to the current file:
PHP Code:
require_once dirname(__FILE__) . '/./includes/TemplateManager.php'// Index's call
require_once dirname(__FILE__) . '/./TemplateManager.php'// Menu's call 
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-01-2009, 08:51 AM   PM User | #3
ajhauser
Regular Coder

 
ajhauser's Avatar
 
Join Date: Nov 2007
Location: Earlville. It's where Earls come from.
Posts: 224
Thanks: 73
Thanked 1 Time in 1 Post
ajhauser is an unknown quantity at this point
I should clarify again, here is an example of what I mean:

http://www.temp1.hangnailproductions.com/index2.php

In this example, my source code is:

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php</title>
</head>

<body>
<?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>
</body>
</html>
But that include address is failing.
Does that make sense?

The reason I ask is because if I base every page off a template with the same include, but save the additional files in a subfolder, I don't think the include will work...
ajhauser is offline   Reply With Quote
Old 05-01-2009, 10:40 AM   PM User | #4
Eran
Regular Coder

 
Join Date: Apr 2009
Location: somewhere over the rainbow
Posts: 105
Thanks: 2
Thanked 14 Times in 14 Posts
Eran is an unknown quantity at this point
i think allow_url_include is not true.


have you read what Fou-Lu wrote???
the "/" before files\folders will get you absolute within your web.
include '/includes/nav.html';

Last edited by Eran; 05-01-2009 at 10:52 AM..
Eran is offline   Reply With Quote
Old 05-01-2009, 12:08 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
PHP Code:
<?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>
Quote:
Originally Posted by http://php.net/function.include
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
You don't actually need to include your file via HTTP ,which is not equivalent of including the path at the server side.

If you want to use the absolute path at the server side, use it like
PHP Code:
include $_SERVER['DOCUMENT_ROOT']."/includes/nav.html"
(just echo the above serve variable to know what's your absolute file path to your roo directory at server side)

I usually define a constant like
PHP Code:
define(_ROOT_$_SERVER['DOCUMENT_ROOT']); 
at the top(or in config file, which would get included in all other files), to make it easier for typing the absolute path, like
PHP Code:
include _ROOT_."/includes/nav.html"
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:32 AM.


Advertisement
Log in to turn off these ads.