473,326 Members | 2,023 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,326 software developers and data experts.

Stupid PHP includes question.

Ben
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.

Jul 17 '05 #1
4 1247
*** 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
--
Jul 17 '05 #2
> 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
Jul 17 '05 #3
Ben
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.

Jul 17 '05 #4
I noticed that Message-ID:
<11**********************@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/
Jul 17 '05 #5

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

Similar topics

2
by: Joe Mowry | last post by:
First off, I'm posting this question for a friend that doesn't have access to the news groups. Problem: Using PHP as the base and has several PHP < includes > for various functions, all this...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
12
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc...
2
by: Ron Weldy | last post by:
I read that you don't need .cs files to deploy but I suppose if you are trying to reconstruct someone's work, you will need these files. Is that correct?
6
by: Adam Smith | last post by:
I have posted this and similar questions repeatedly and can't even raise a single response. I am being led to believe that this then 'Must be a stupid question' although people say that there is no...
1
by: antjudy01 | last post by:
Hi, I was thinking about joining the MSFT Empower program which includes "5 licenses to Visual Studio 2005 Professional Edition". My question is how do I get the media to actually install the...
3
by: Jeff | last post by:
I have an asp page. Now I know this is sortof OT, but I am not sure of the forum to get an answer. I have an include on this asp page <!-- #include file=\forum\includes\consts-inc.asp --> ...
6
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the...
2
by: Lynx101 | last post by:
Hi, Is this a stupid question? Senario: Two tables linked together with an ID number. Question: When using a combo box, which refences another table by indexed autonumber, is there a way to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.