Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Relative Paths With include()

MBS
Guest
 
Posts: n/a
#1: Nov 22 '05
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.


Rik
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Using Relative Paths With include()


MBS wrote:[color=blue]
> 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![/color]

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

Grtz,

Rik


MBS
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Using Relative Paths With include()


"Rik" <luiheidsgoeroe@hotmail.com> wrote in news:dlf7km$6fa$1
@netlx020.civ.utwente.nl:
[color=blue]
> MBS wrote:[color=green]
>> 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![/color]
>
> It does, works perfectly here in PHP4 and PHP5.
> Search your code for errors.
>
> Grtz,
>
> Rik
>
>
>[/color]

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.
J.O. Aho
Guest
 
Posts: n/a
#4: Nov 22 '05

re: Using Relative Paths With include()


MBS wrote:[color=blue]
> 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.[/color]

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
MBS
Guest
 
Posts: n/a
#5: Nov 22 '05

re: Using Relative Paths With include()


"J.O. Aho" <user@example.net> wrote in
news:3u0mikFuo1hoU1@individual.net:
[color=blue]
> MBS wrote:[color=green]
>> 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.[/color]
>
> 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.[/color]

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!
[color=blue]
>
> 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
>[/color]

Jerry Stuckle
Guest
 
Posts: n/a
#6: Nov 22 '05

re: Using Relative Paths With include()


MBS wrote:[color=blue]
> "J.O. Aho" <user@example.net> wrote in
> news:3u0mikFuo1hoU1@individual.net:
>
>[color=green]
>>MBS wrote:
>>[color=darkred]
>>>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.[/color]
>>
>>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.[/color]
>
>
> 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!
>
>[color=green]
>>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
>>[/color]
>
>[/color]

On Apache:

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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Etienne Marais
Guest
 
Posts: n/a
#7: Nov 22 '05

re: Using Relative Paths With include()


> Is there anyway to get the name and directory of the currently executing[color=blue]
> 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.[/color]

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 :(
Sandman
Guest
 
Posts: n/a
#8: Nov 22 '05

re: Using Relative Paths With include()


In article <437b23d2$0$27941$892e7fe2@authen.yellow.readfreen ews.net>,
MBS <mbs@mbs.net> wrote:
[color=blue]
> "Rik" <luiheidsgoeroe@hotmail.com> wrote in news:dlf7km$6fa$1
> @netlx020.civ.utwente.nl:
>[color=green]
> > MBS wrote:[color=darkred]
> >> 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![/color]
> >
> > It does, works perfectly here in PHP4 and PHP5.
> > Search your code for errors.
> >
> > Grtz,
> >
> > Rik
> >
> >
> >[/color]
>
> 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.[/color]

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]
MBS
Guest
 
Posts: n/a
#9: Nov 22 '05

re: Using Relative Paths With include()


Sandman <mr@sandman.net> wrote in
news:mr-FB0161.16223016112005@individual.net:
[color=blue]
> In article <437b23d2$0$27941$892e7fe2@authen.yellow.readfreen ews.net>,
> MBS <mbs@mbs.net> wrote:
>[color=green]
>> "Rik" <luiheidsgoeroe@hotmail.com> wrote in news:dlf7km$6fa$1
>> @netlx020.civ.utwente.nl:
>>[color=darkred]
>> > 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
>> >
>> >
>> >[/color]
>>
>> 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.[/color]
>
> 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.
>
>
>[/color]

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
Savut
Guest
 
Posts: n/a
#10: Nov 22 '05

re: Using Relative Paths With include()


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.

guitarromantic@gmail.com
Guest
 
Posts: n/a
#11: Nov 22 '05

re: Using Relative Paths With include()


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.

Etienne Marais
Guest
 
Posts: n/a
#12: Nov 22 '05

re: Using Relative Paths With include()


Savut wrote:
[color=blue]
> 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.[/color]

I whish I knew this ages ago, thanks :)

Etienne Marais
Closed Thread