Bluehost.com Web Hosting $6.95

Stupid PHP includes question.

This is a discussion on Stupid PHP includes question. within the PHP Language forums, part of the PHP Programming Forums category; This is probably a very simple question (I hope), but my usually great web searching abilities have not turned up ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-20-2005
Ben
 
Posts: n/a
Default Stupid PHP includes question.

This is probably a very simple question (I hope), but my usually great
web searching abilities have not turned up the answer I am looking for.

I have a php page that includes a file for example index.php has an
include for menu.htm. Inside menu.htm are some images in the same
directory. All works great in that scenario.

Here's where the trouble begins. If I want to include menu.htm inside
a php file that is in another directory (say testdir/page.php) it is
unable to load the images because it is assuming the images are in the
same directory as page.php... it does not know the images are in the
directory above (the same directory that menu.php is in).

So when including a file that is contained within another directory,
the including page (testdir/page.php) uses the currenty directory as
the starting point when evaluating relative paths... because of this
it's looking for the images in the same directory that it's in..
instead of in the above directory like menu.htm indicates. Not sure if
that makes sense... here is an example of the pages / directories.

for example:

c:\php\index.php
c:\php\menu.htm
c:\php\menuimage.jpg
c:\php\testdir\page.php

==============================
code for index.php [WORKS]:
==============================
<? include ("menu.htm") ?>
==============================

==============================
code for page.php [Does not work. cannot load images]:
==============================
<? include ("../menu.htm") ?>
==============================

/\
|
|
\/

even though menu.htm is in the same directory as the .jpg file it is
looking for the .jpg in the directory it's being called from
(testdir)... the same directory that the including page (page.php) is
in.

Hope this makes sense. Is there a php function that can correc this
easily or am I using the include wrong? I have been able to correct
this by writing code that checks the URL and changes the reletive path
depending on which dir the including page is in... but that seems way
to complicated for this issue. I know there must be a simpler way.

Reply With Quote
  #2 (permalink)  
Old 02-20-2005
Alvaro G. Vicario
 
Posts: n/a
Default Re: Stupid PHP includes question.

*** Ben escribió/wrote (20 Feb 2005 05:56:30 -0800):
> So when including a file that is contained within another directory,
> the including page (testdir/page.php) uses the currenty directory as
> the starting point when evaluating relative paths... because of this
> it's looking for the images in the same directory that it's in..
> instead of in the above directory like menu.htm indicates. Not sure if
> that makes sense... here is an example of the pages / directories.


It's easier than you think. Including a file is exactly the same as opening
it in an editor, copying it to clipboard and pasting in your current
script.

If you want to use relative paths, they must be relative to the document
where you are pasting the text. If you're planning to paste it in documents
located if different directories, then you must use absolute paths.


--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Manda tus dudas al grupo, no a mi buzón
-+ Send your questions to the group, not to my mailbox
--
Reply With Quote
  #3 (permalink)  
Old 02-20-2005
manu
 
Posts: n/a
Default Re: Stupid PHP includes question.

> for example:
>
> c:\php\index.php
> c:\php\menu.htm
> c:\php\menuimage.jpg
> c:\php\testdir\page.php
>
> ==============================
> code for index.php [WORKS]:
> ==============================
> <? include ("menu.htm") ?>
> ==============================
>
> ==============================
> code for page.php [Does not work. cannot load images]:
> ==============================
> <? include ("../menu.htm") ?>
> ==============================
>
> /\
> |
> |
> \/
>
> even though menu.htm is in the same directory as the .jpg file it is
> looking for the .jpg in the directory it's being called from
> (testdir)... the same directory that the including page (page.php) is
> in.
>
> Hope this makes sense. Is there a php function that can correc this
> easily or am I using the include wrong? I have been able to correct
> this by writing code that checks the URL and changes the reletive path
> depending on which dir the including page is in... but that seems way
> to complicated for this issue. I know there must be a simpler way.
>


I don't know any (antive) function that would do it for you...
Moreover I think it's a good thing to put some var in path..
The way I use is to define all my directory as constant in first
included file, so, I can modify this declaration if I change any path..
(it could include changing entry page, even if I dislike this)
After this, You just have to use this constant.. : <img
src="<?=IMAGEDIR;?>myimage.ext"> which would take care of relative path

sorry for my poor english
Reply With Quote
  #4 (permalink)  
Old 02-20-2005
Ben
 
Posts: n/a
Default Re: Stupid PHP includes question.

Doh, I was afraid I would have to use absolute paths. I thought I came
across a function once that will get you the absolute path of a file...
but even if that existed I would still be stuck if using that function
inside the include file when calling it from another directory.

Reply With Quote
  #5 (permalink)  
Old 02-20-2005
Geoff Berrow
 
Posts: n/a
Default Re: Stupid PHP includes question.

I noticed that Message-ID:
<1108916148.713623.206170@l41g2000cwc.googlegroups .com> from Ben
contained the following:

>Doh, I was afraid I would have to use absolute paths.


Why is this a problem?
<img src="/images/image.jpg">

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:58 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0