Jump to content

include function not working in other directories


saturnstroll

Recommended Posts

Hello, I successfully used the include function on http://mrconcreteblockhouse.com/index.php which has 2 include files:

<?php include("top.php"); ?>

and

<?php include("bottom.php"); ?>

The index works just fine.

 

However when I try to put the same code in http://mrconcreteblockhouse.com/f/found ... b.html.php it doesn't work.

I then tried placing copies of top.php and bottom.php in the same /f/ directory, but still doesn't show the top or bottom.

 

Then I tried defining the path to the directory:

<?php define('PATH', '/www/mrconcreteblockhouse/httpdocs/'); ?>

<?php include(PATH . "top.php"); ?>

 

Still doesn't work.

Any ideas?

Link to comment
Share on other sites

Your include file is assuming the file is on the same level.

 

If your script is in another folder, you have to adjust your include to allow for that.

 

If it is in the same folder, there is no reason why it wouldn't work.

Link to comment
Share on other sites

Your include file is assuming the file is on the same level.

If your script is in another folder, you have to adjust your include to allow for that.

If it is in the same folder, there is no reason why it wouldn't work.

 

Exactly. I have it top.php and bottom.php sitting in the same directory /f/

I don't see why it's not working.

Link to comment
Share on other sites

This is the way  I include things I want showing on every page regardless of where it lives in the directory structure.

<?php
include($_SERVER['DOCUMENT_ROOT'].'/top.php');
include($_SERVER['DOCUMENT_ROOT'].'/bottom.php');
?>

 

Hi, so if I cut/paste that will it work, or do I need to make modifications, or move files to a particular directory/area?

Link to comment
Share on other sites

if top.php and bottom.php are in the root directory of your site they will work. Basically, the directory that serves your index page. If they are not there, then either move them there or adjust the path to them.

 

the superglobal $_SERVER['DOCUMENT_ROOT'] starts in the directory where your index page is served from.

 

 

Nate

Link to comment
Share on other sites

hmmm i would try....

 

include "../top.php"; //means go up a level and find top.php
include "../bottom.php";

 

If that doesnt work tell us the error wihch your getting

 

But if the page that is calling these is /services/some_service/index.php  that will look in /services ..    if these files are located at /top.php  & /bottom.php  then the ../ method will fail.

 

I fought with this exact thing for a long time. I wanted to find the equivalent of HTML's  /  "start at root" type path. Using $_SERVER['DOCUMENT_ROOT'] was exactly that. Calling this with every include stopped every single include problem I had experienced.

 

Just my $.02 :)

 

Nate

Link to comment
Share on other sites

For every page that calls top.php/bottom.php use the relative path. So if you are in /index.php use include('top.php'); If you are in /subdir/page.php, use include('../top.php');

 

Then, inside top.php and bottom.php, if you need to include/access other files, use this method of getting to them:

 

top.php

<?php
  //include the functions
  include(dirname(__FILE__).'/functions.php');
?>

 

This way no matter where you include top.php from, it will work.

Link to comment
Share on other sites

what do you mean HTML equivalent? I see no HTML

 

Maybe you are looking for http://www.w3schools.com/tags/tag_base.asp ?

 

In HTML, you can denote a path as /path/to/a/folder. 

 

By using the / at the start, this tells HTML to start at root.

 

In php the closest thing I found is $_SERVER['DOCUMENT_ROOT'].

 

This allows me to add that to an include path and I NEVER have to worry about the path being wrong. I fought with this for a LONG TIME. This is the best solution I could come up with.

 

 

Nate

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.