Jump to content

PHP include with subfolders. Really appreciate some help.


Major_Disaster

Recommended Posts

Hey guys,

 

Im in the middle of making a website and trying to learn php as i go. Ill try to explain as clearly as i can, but for those who want to cut to the chase, how do i do a php include from a subfolder, calling a file from the root directory?

 

The long winded version: Ive got a site in the following structure:

index.php

header.php

footer.php

products/page1.php

products/page2.php

products/page3.php

style.css

 

My header.php includes the following:

 

<head>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css" type="text/css" media="all">
<title><?php echo $title; ?></title>
<meta name="title" content="<?php echo $metatitle; ?>">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
</head>

 

So, in my index.php i have:

<?php
$title ="Insert title here";
$metatitle ="Insert title here";
$description ="Insert description here";
$keywords ="example, key, word, here";
include ('header.php');

 

Thats great and works well. But when i tru to do the same from products/page1.php, i get a whole load of problems because the include is looking in the wrong place. I can kinda get it working by using include('../header.php') - but that doesnt apply my css, as that loads the header.php allright, but then just looks for the css in products/style.css! So, i kinda understand what is going on, but i dont know how to fix it.

 

Ive been searching loads, and ive seen something like DOCUMENT_ROOT, which i think could work, but i dont know the actual code to use it. Im thinking, something like

 

<head>
<link rel="icon" href="[DOCUMENT_ROOT]/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="[DOCUMENT_ROOT]/style.css" type="text/css" media="all">
<title><?php echo $title; ?></title>
<meta name="title" content="<?php echo $metatitle; ?>">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
</head>

 

But when i tried that on my XAMPP test server, firefox read it as C:\XAMPP\style.css - and was unable to load it?!

 

So, what on earth can i do?

Cheers for any help. Seriously, im really getting annoyed by this and any help would be greatly appreciated.

I usually get around this by using absolute paths to the stuff in a header/footer file. If the site is at the root a URL, like so:

 

http://www.hostname.com/index.php

http://www.hostname.com/products/page1.php

etc..

 

then it's easy, just change href="style.css" to href="/style.css". But, if this website "bundle" can be in any subdir like

 

http://www.hostname.com/subdir/index.php

http://www.hostname.com/subdir/products/page1.php

etc..

 

I usually just create a variable at the top of header.php called $prefix, set it to the subdir, and then use it to make absolute paths:

 

<?php
  $prefix = "/subdir/";
?>
<head>
<link rel="icon" href="<?php print $prefix;?>favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="<?php print $prefix;?>style.css" type="text/css" media="all">
<title><?php echo $title; ?></title>
<meta name="title" content="<?php echo $metatitle; ?>">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
</head>

You can modify your header.php by replacing

<link rel="stylesheet" href="style.css" type="text/css" media="all">

with

<link rel="stylesheet" href="<?php echo $css_path ?>" type="text/css" media="all">

and then in index.php use an extra line

$css_path = "style.css";

and in products/page1.php you should write

$css_path = "../style.css";
include ('../header.php');

 

 

 

Thanks for the reply rhasan_82 - really helpful. It works! (kinda)

 

The css loads with /style.css, but... My nav bar (the links of which are stored in header.php, and the design in style.css) contains some images. These images load find from index.php, and the site is perfect. But from products/page1.php the images dont load.

 

Im sure this is another case of dogy file links, but what should i have to make it work from all pages?

 

Thanks again for the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.