PHP: Includes directory, strucuture, and/or path issue - php

Problem is very simple.. It would seem.
Error:
Warning: include(includes/navigation.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sub_page\about.php on line 27
Warning: include(): Failed opening 'includes/navigation.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\sub_page\about.php on line 27
I am redoing a site. Currently I am splicing it into PHP. Same way I have also ways done. Except this time I am making it more structured. Example of my code.
<!-- Start Navigation -->
<?php include 'includes/navigation.php'; ?>
<!-- End Navigation -->
<!-- Start Top Menu -->
<?php include 'includes/top_menu.php'; ?>
<!-- End Top Menu -->
<!-- Start Promo Menu -->
<?php include 'includes/pro_menu.php'; ?>
<!-- End Promo Menu -->
So lets that the file navigation is in includes folder which is located in the root. Lets say that the code above is on the services page which is '2' levels in. Not 1'. Typically i have it only '1' lvl of dir in so simply changing
<?php include 'includes/navigation.php'; ?>
to this
<?php include '../includes/navigation.php'; ?>
worked fine.
The above code is on the services page which is located at
/sub_page/services/services.php
If i moved the services.php file from the above dir path to 1 lvl close to root like so
/sub_page/services.php
and change the path on the include to
<?php include '../includes/navigation.php'; ?>
it works fine. I need to be able to call the files from anywhere. Does anyone know how to do this? Thanks!
EDIT: Will use the following as an example. I was pulling a path error on this line
<?php include 'sub_page/about_us/includes/meta.php'; ?>
I was able to use the following instead
<?php include __DIR__ . "/includes/meta.php"; ?>
to fix it. However if i tried to add or take away a directory level from the path it would again return an error. The same thing will not work for any of the other lines/paths.
before
<?php include 'includes/navigation.php'; ?>
after
<?php include __DIR__ . "/navigation.php"; ?>
Any idea?

Set an include path for PHP to "look" for these files: http://php.net/manual/en/function.set-include-path.php or use an absolute path.

If I understand you correctly you want to include a file (services.php) in other files which are on different levels. The included file itself includes some other files.
You must use absolute paths in services.php, e.g.
include __DIR__ . "/navigation.php";
Then it is irrelevant from which directory level you include services.php. __DIR__ will always have the correct path of services.php, no matter where services.php is included.
EDIT: Maybe even clearer...
Assuming directory tree:
<htdocs>/index.php
<htdocs>/includes/meta.php
<htdocs>/includes/navigation.php
<htdocs>/sub_page/about.php
navigation.php is to be included by meta.php.
meta.php is to be included by index.php and about.php.
Paths for includes:
meta.php: include __DIR__ . "/navigation.php";
index.php: include "includes/meta.php";
about.php: include "../includes/meta.php";
Any other file can now include meta.php with a path depending on its directory level.
The __DIR__ in meta.php will always provide the correct path to navigation.php.

Related

Add Header, Footer and navbar files on multiple pages

Let says below are my web directories:
From web searching i get to use include or require_once and something similar.But I'm confuse in different directory causes much of directory error.
And my question is, how to include Navbar or Footer or Specific Pages in different directory.
Condition: Same directory as above;
For header,navBar and footer you should used simple
include(page/header.php);
About require and include function Reference
The require() function is identical to include(), except that it
handles errors differently. If an error occurs, the include() function
generates a warning, but the script will continue execution. The
require() generates a fatal error, and the script will stop.
But for image,css files and other js files you should follow these steps
Create config.php file
add this in config.php
define('BASE_URL', 'http://your_website_url');//OR define('BASE_URL', 'localhost')
you can use this path like
<?php
include('config.php');//add this code into top of the page
?>
and finally you can used where you want, like
For Style sheet
<link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
Create a config.php file in your home directory.
<?php
/* Saves the directory path to base directory as `BASE_DIR`
Example: /var/user1/home/public_html/
'Base directory' is where your homepage/index.php is located */
define('BASE_DIR', dirname(__FILE__) . '/');
?>
now just require this config.php file
<?php
require_once('config.php'); //add this line at top of your pages
// No need to include this inside 'header.php' and 'footer.php'
?>
Now u can include files easily, use it in index.php as follows:
<?php
require_once('config.php');
require(BASE_DIR . 'query/conn.php');
// becomes '/var/user1/home/public_html/query/conn.php'
include(BASE_DIR . 'pages/header.php');
include(BASE_DIR . 'pages/navbar.php');
include(BASE_DIR . 'pages/footer.php');
?>
Note: Similar approach is used by WordPress (learn more)

How to include php file in a php file

Hey I would like to include a navigation.php file in my website, but i cant figure out how to include an external file in a .php file, and the only way i could find to include the navigation.php into the files was to make a copy of it in each directory.
<?php include("navigation.php"); ?> works when i make a copy of the navigation.php in each directory.
<?php include("http://www.mysite.net/format/navigation.php"); ?> gives me an error.
I would like to only have one of these navigation.php files in a "Format" directory and then make each page fetch the file and display it.
Also ill be monitoring this question for a while so just ask me if you need any additional info.
Put that file one place. Then refer to it in each file by its proper path.
// navigation.php is in the same directory
<?php include("navigation.php"); ?>
<?php include("./navigation.php"); ?>
// navigation.php is one directory below the current directory
<?php include("../navigation.php"); ?>
// navigation.php is two directories below the current directory
<?php include("../../navigation.php"); ?>
But this can get tedious if you have lots of files in lots of directories. The best way to do this is to use the full path to the file on the file system:
// Full path relative to the root of the account
<?php include("/full/path/to/navigation.php"); ?>
But this can get tedious if the path changes. So define a variable or constant to hold that value for you so you only need to change it in one place:
// In a config file
define('INCLUDE_PATH', '/full/path/to/');
// In each page after you include your config file
<?php include(INCLUDE_PATH . "navigation.php"); ?>

PHP include Statement Issue

I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.
Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>
Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.

Manage multiple include files in PHP

I have a main.php file which has the following three included php files:
<!--TAB 1--->
<?php include "tab1.php"; ?>
<!--TAB 2--->
<?php include "tab2.php"; ?>
<!--TAB 3--->
<?php include "tab3.php"; ?>
When I open main.php in Dreamweaver, I have access to these 3 included files. My question is: What about files that are included inside one of these include files?
For example, the tab2.php file reference these php files:
<?php include "a.php"; ?>
<?php include "b.php"; ?>
<?php include "c.php"; ?>
In order to be able to edit these included files which are inside another include file, what needs to be done? Will I need to open those directly?
What I would like to be able to do is open main.php and then be able to add all 6 included files:
tab1.php
tab2.php
tab3.php
a.php
b.php
c.php
It seems as though I only have direct access to the first-level include files. Any suggestions? Thanks.
If you're using Dreamweaver CS5, it has an (default enabled) option to show includes in a line below the file tabs. If you have an include that has other includes, the only way of editting this includes directly from this file is to press CTRL+D in the include, then Dreamweaver will show a popup for you to select the includes to edit and it will open the selected one in a tab.

How to display same header and footer on different pages?

I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>
Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -
Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1
Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1
This makes testing very tedious as I have to upload to the server for every minor change.
Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.
What is the correct way for including header and footer files?
the correct way to include any file is the include() or require() function. Wordpress uses a get_header() function because the header is more then just 1 file, so they created a function for outputting it.
The problem you have seems to be a problem with the $_SERVER variable. It has been quite a long time since I've worked with PHP, but what I would advise you to do is just use relative paths. For example, if the header.php and footer.php files are in the same directory as your index.php, you can just do:
<?php include("header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include('footer.php') ?>
Simple and useful way (I use this in my all projects):
// find Base path
define( 'BASE_PATH', dirname(__FILE__) );
// include files
include BASE_PATH . '/header.php';
include BASE_PATH . '/footer.php';
It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.
edit:
<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
//On Production
$rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
//On Dev server
$rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}
<?php include($rootDir.'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($rootDir.'/footer.php') ?>
?>

Categories