/    Sign up×
Community /Pin to ProfileBookmark

Relative path query

Hi,

This is a very basic question, though I am a very basic person I’m afraid!

I’m using the following to insert a menu at the top of my php web pages:

<?php include “header.php” ?>

The problem I’m having is that the files on the site that are situated in sub folders don’t find the file. I get round this by putting in the full path to the file when including it eg:

<?php include “http://www.mysite.com/header.php” ?>

and this works but it feels messy. Is there another way?

Also, when I use this full address, I have to relink all the graphics used in the file with the full path too (which is taking me ages!).

Any help would be really appreciated.

Thanks,

Andy

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 23.2008 — When you include files use the server path, not a URL. e.g. /htdocs/yoursite/public/etc not http://yoursite.com/
Copy linkTweet thisAlerts:
@barbeluk1authorApr 23.2008 — Thanks - but if my header file is at the root of the domain I'm not sure what path to use?

Andy
Copy linkTweet thisAlerts:
@barbeluk1authorApr 24.2008 — Hi again,

I tried referencing the path '/httpdocs/header.php' but get this message:

Warning: main(): open_basedir restriction in effect. File(/httpdocs/header.php) is not within the allowed path(s): (/var/www/vhosts/tenchfishingworld.co.uk/httpdocs:/tmp) in /var/www/vhosts/tenchfishingworld.co.uk/httpdocs/index2.php on line 22

Is there a way of forcing a file to look in the root of the domain?

Thanks,

Andy
Copy linkTweet thisAlerts:
@bokehApr 24.2008 — Try:[code=php]include($_SERVER['DOCUMENT_ROOT'] . '/header.php');[/code]
Copy linkTweet thisAlerts:
@barbeluk1authorApr 24.2008 — Thanks - it now finds the header file though the links to the graphics are broken though they've not moved anywhere.
Copy linkTweet thisAlerts:
@SyCoApr 24.2008 — To understand how paths and files interact first make a simple file that includes a header.

simple.php
[code=php]<?
include "header.php";
?>

<img src="mypic1.jpg">
</body>[/code]


header.php
[code=php]<html>
<body>
<img src="mypic2.jpg">
[/code]


Create a folder with a sub folder and save simple.php, header.php and both image files in the subfolder. All 4 files should be in the same folder. Visit the URL for simple.php. You should have a very simple page with 2 images displayed. Make sure you get this part working before going any further.

Now create 2 sub folders called includes and images.

One at a time move the images in their sub folder and change their paths. Then move the include in to its sub folder. Play around moving files, and changing the paths. Move the include and images to folders higher up the tree from the folder simple is in too. Put an image or an include file in the root and see what happens.

It's important that each time you move a file into a sub folder, check simple.php through a browser and fix the script so you have your 2 images displaying correctly.

This is a 10 minute exercise and when you're done you will completely understand how paths work.
Copy linkTweet thisAlerts:
@barbeluk1authorApr 24.2008 — Thanks SyCo - I'll give this a go in the morning.

Regards,

Andy
×

Success!

Help @barbeluk1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...