473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2241
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.utwente.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('includes/another.php');

include in another.php:
include('includes/yetanother.html');
//Aho
Nov 22 '05 #4
MBS
"J.O. Aho" <us**@example.net> 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('includes/another.php');

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


Nov 22 '05 #5
MBS wrote:
"J.O. Aho" <us**@example.net> 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('includes/another.php');

include in another.php:
include('includes/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*******@attglobal.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("include_path",get_include_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_part1a.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_URI"]
$_SERVER["SCRIPT_NAME"]
$_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***********************@authen.yellow.readfreen ews.net>,
MBS <mb*@mbs.net> wrote:
"Rik" <lu************@hotmail.com> wrote in news:dlf7km$6fa$1
@netlx020.civ.utwente.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.html" 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***********************@authen.yellow.readfreen ews.net>,
MBS <mb*@mbs.net> wrote:
"Rik" <lu************@hotmail.com> wrote in news:dlf7km$6fa$1
@netlx020.civ.utwente.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.html"
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
Would that fix my issues? The files i include are in root, all the
files that use it (besides index) are in subdirs off root, so i can
make references like include '../config.php'; except for my index.php,
which i had to place in /home/ and use a redirect for.

Nov 22 '05 #11
Savut wrote:
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.


I whish I knew this ages ago, thanks :)

Etienne Marais
Nov 22 '05 #12

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

Similar topics

7
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...
1
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...
7
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...
2
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...
2
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...
19
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...
6
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...
15
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.