473,573 Members | 5,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Relative Paths With include()

MBS
I am playing around with some PHP code. I want to put in an include()
function so I can include existing HTML code and output it to the browser.

Lo and behold PHP does not support relative paths with the include()
function! (How shortsighted can you get?) Is there any way at all to use
relative paths with include()? Any hacks? If I use an absolute filepath,
everything is fine. But I don't want to do that--I can't do that. I want
to use the filepath relative to the currently executing PHP file.

Somewhere I read something about using an .htaccess file and putting
something in it that will add an "include" for the HTML file I want to
include without me having to change php.ini (I won't have access to php.ini
on the production server anyway.). That way I could just drop it in my
webroot directory and everything would be fine.

Help will be appreciated.

Thanks.

Nov 22 '05 #1
11 2254
Rik
MBS wrote:
I am playing around with some PHP code. I want to put in an include()
function so I can include existing HTML code and output it to the
browser.

Lo and behold PHP does not support relative paths with the include()
function!


It does, works perfectly here in PHP4 and PHP5.
Search your code for errors.

Grtz,

Rik
Nov 22 '05 #2
MBS
"Rik" <lu************ @hotmail.com> wrote in news:dlf7km$6fa $1
@netlx020.civ.u twente.nl:
MBS wrote:
I am playing around with some PHP code. I want to put in an include()
function so I can include existing HTML code and output it to the
browser.

Lo and behold PHP does not support relative paths with the include()
function!


It does, works perfectly here in PHP4 and PHP5.
Search your code for errors.

Grtz,

Rik


No, it does not work.

I have a file in the directory of the currently executing php code. One
would assume the following would work:

include 'myfile.hmtl';

But it does not work.

I'm using PHP5 on WinXP and Apache 2.
Nov 22 '05 #3
MBS wrote:
I am playing around with some PHP code. I want to put in an include()
function so I can include existing HTML code and output it to the browser.

Lo and behold PHP does not support relative paths with the include()
function! (How shortsighted can you get?) Is there any way at all to use
relative paths with include()? Any hacks? If I use an absolute filepath,
everything is fine. But I don't want to do that--I can't do that. I want
to use the filepath relative to the currently executing PHP file.


include("../some/other/directory/the.www.html");

This works as long as as you don't make an include from an included file, as
the path is always counted from the first page.

if you have the file index.php that includes the page includes/another.php,
which in it's turn want to include yetanother.html , you still have to include
it as if you did the include from index.php

include in index.php:
include('includ es/another.php');

include in another.php:
include('includ es/yetanother.html ');
//Aho
Nov 22 '05 #4
MBS
"J.O. Aho" <us**@example.n et> wrote in
news:3u******** ****@individual .net:
MBS wrote:
I am playing around with some PHP code. I want to put in an
include() function so I can include existing HTML code and output it
to the browser.

Lo and behold PHP does not support relative paths with the include()
function! (How shortsighted can you get?) Is there any way at all
to use relative paths with include()? Any hacks? If I use an
absolute filepath, everything is fine. But I don't want to do
that--I can't do that. I want to use the filepath relative to the
currently executing PHP file.
include("../some/other/directory/the.www.html");

This works as long as as you don't make an include from an included
file, as the path is always counted from the first page.


Ah! There is the problem.

Just now I noticed that when I check __FILE__ in the PHP code in question
it does not return the current file but returns one of the includees.

Is there anyway to get the name and directory of the currently executing
file regardless of whether or not it has been included in another?
Because if you have multiple files including currentfile.php , you're
going to need to tweak your include() statement for each one unless there
is some way to work around that.

Thank you for the information!

if you have the file index.php that includes the page
includes/another.php, which in it's turn want to include
yetanother.html , you still have to include it as if you did the
include from index.php

include in index.php:
include('includ es/another.php');

include in another.php:
include('includ es/yetanother.html ');
//Aho


Nov 22 '05 #5
MBS wrote:
"J.O. Aho" <us**@example.n et> wrote in
news:3u******** ****@individual .net:

MBS wrote:
I am playing around with some PHP code. I want to put in an
include() function so I can include existing HTML code and output it
to the browser.

Lo and behold PHP does not support relative paths with the include()
function! (How shortsighted can you get?) Is there any way at all
to use relative paths with include()? Any hacks? If I use an
absolute filepath, everything is fine. But I don't want to do
that--I can't do that. I want to use the filepath relative to the
currently executing PHP file.


include("../some/other/directory/the.www.html");

This works as long as as you don't make an include from an included
file, as the path is always counted from the first page.

Ah! There is the problem.

Just now I noticed that when I check __FILE__ in the PHP code in question
it does not return the current file but returns one of the includees.

Is there anyway to get the name and directory of the currently executing
file regardless of whether or not it has been included in another?
Because if you have multiple files including currentfile.php , you're
going to need to tweak your include() statement for each one unless there
is some way to work around that.

Thank you for the information!

if you have the file index.php that includes the page
includes/another.php, which in it's turn want to include
yetanother.ht ml, you still have to include it as if you did the
include from index.php

include in index.php:
include('incl udes/another.php');

include in another.php:
include('incl udes/yetanother.html ');
//Aho



On Apache:

include ($_SERVER['DOCUMENT_ROOT']) . "/relative/to/root/directory.html"

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 22 '05 #6
> Is there anyway to get the name and directory of the currently executing
file regardless of whether or not it has been included in another?
Because if you have multiple files including currentfile.php , you're
going to need to tweak your include() statement for each one unless there
is some way to work around that.


For larger projets I start with index.php in the topmost directory, and
have a directory structure of the like:

../include
../include/extension1/
../include/extension2/
../include/extra_lib1/

../dta
../dta/excel_data.xls
../dta/xml_data.xls

index.php simply sets the include path:

ini_set("includ e_path",get_inc lude_path().":. :./include/:./include/extension1:./inc.....");

If you use some code over and over, say include/extra_lib1,
move it out of the project tree up into the document root
and edit php.ini to point to that directory.

Since I have been coding OO (before PHP 5) entry into the
system is ALLWAYS via index.php so the problem you mention
is not applicable to me. Even without OO you can branch
your code from index.php so that you don't load sub_program_par t1a.php
into the url, but include/require as needed, so the paths are
allways set and you just include what you need without including
the directory in the include/require statement.

If you need to find info about the main running script, check out:

$_SERVER["REQUEST_UR I"]
$_SERVER["SCRIPT_NAM E"]
$_SERVER["PHP_SELF"]

to find info about the current working directory (the directory relative
from where anything needs to be included, regardless the running script)
try)

$cwd=`pwd`; // Linux
$cwd=exec("dir" ); // or something like this for windows.

I'm shure there must be something to find information about the
'running sub script' but I don't know what it is :(
Nov 22 '05 #7
In article <43************ ***********@aut hen.yellow.read freenews.net>,
MBS <mb*@mbs.net> wrote:
"Rik" <lu************ @hotmail.com> wrote in news:dlf7km$6fa $1
@netlx020.civ.u twente.nl:
MBS wrote:
I am playing around with some PHP code. I want to put in an include()
function so I can include existing HTML code and output it to the
browser.

Lo and behold PHP does not support relative paths with the include()
function!


It does, works perfectly here in PHP4 and PHP5.
Search your code for errors.

Grtz,

Rik


No, it does not work.

I have a file in the directory of the currently executing php code. One
would assume the following would work:

include 'myfile.hmtl';

But it does not work.

I'm using PHP5 on WinXP and Apache 2.


If that's cut'n'paste from your script, then you have a typo in the extension.

Other than that, PHP can be set not to use "." for include dir - check your
php.ini for your include dir. Even then, files earlier in the include path
supersedes later ones, so if you have a "myfile.htm l" earlier in the paths it
will be included instead of the relative one.

--
Sandman[.net]
Nov 22 '05 #8
MBS
Sandman <mr@sandman.net > wrote in
news:mr******** **************@ individual.net:
In article <43************ ***********@aut hen.yellow.read freenews.net>,
MBS <mb*@mbs.net> wrote:
"Rik" <lu************ @hotmail.com> wrote in news:dlf7km$6fa $1
@netlx020.civ.u twente.nl:
> MBS wrote:
>> I am playing around with some PHP code. I want to put in an
>> include() function so I can include existing HTML code and output
>> it to the browser.
>>
>> Lo and behold PHP does not support relative paths with the
>> include() function!
>
> It does, works perfectly here in PHP4 and PHP5.
> Search your code for errors.
>
> Grtz,
>
> Rik
>
>
>


No, it does not work.

I have a file in the directory of the currently executing php code.
One would assume the following would work:

include 'myfile.hmtl';

But it does not work.

I'm using PHP5 on WinXP and Apache 2.


If that's cut'n'paste from your script, then you have a typo in the
extension.

Other than that, PHP can be set not to use "." for include dir - check
your php.ini for your include dir. Even then, files earlier in the
include path supersedes later ones, so if you have a "myfile.htm l"
earlier in the paths it will be included instead of the relative one.


Well, what I've got is:

echo 'HTML code'
include 'myfile.html'
echo 'more HTML code'

It works just fine with an absolute filepath. It just doesn't work with
the relative one.

As someone stated, it must be because the file I have the include() in is
included by anothe file and I must base the filepath off of the first
file.

Thank you and thank EVERYONE for the help. I appreciate it.

MBS
Nov 22 '05 #9
I always use dirname(__file_ _) which will return the current path to
this file, even if it is included on another one. You never get wrong
with this one.

Nov 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2770
by: Doug | last post by:
If I were to write an include with a relative path like include("../conf/config.php"); What is the use? As far as I understand it, the path is relative to the first script that is called by php. In other words, if the current working directory is /www/ and you were
1
6813
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must...
7
2825
by: mark | last post by:
I am designing a website at the moment and looking at the difference between relative and absolute url links which is driving me crazy! I would like to use relative paths, but it is proving very restrictive as to how I design the file structure when it comes to including files. I currently have something like below: folder1 folder2 images...
2
3224
by: hawks5999 | last post by:
I have setup Software Update Service servers at two locations in our company (SUSserver1 and SUSserver2). I am modifying the web interface on one server so that it will allow me to control both servers from page. The problem I'm running into is that the synchronize and approve functions are both .asp pages that use 'include virtual='...
2
1850
by: Joe | last post by:
Hi, can someone tell me how to set up relativ paths in VS2003 C++ ? I have some source with a tree directory structure that segments include files in various directories: #include "../include/config.h" /* Keep this first */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/complex.h" #include "../include/su3.h"
19
5062
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other things, like php code), which resolves the correct image path when opened under / but when x.php is read into a file under /dir the image no longer...
6
2731
by: Max | last post by:
Hello - Just switched to PHP 5 and just realized that all my scripts with relative paths for require statements no longer work unless I put the absolute path. Usually, if the file is in the same directory I would us the following statement: require 'file.php'; Now I'm having to use:
15
6442
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would make it much easier to slap modualar blocks of markup into page frameworks, which may change and so forth. And the few extra bytes, which even for a...
6
2489
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php |__/utils |__/logs/logger.inc When I run foo.php I get the following error:
0
7709
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8218
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.