php include from main folder to a file in different folder - php

in my root folder of my domain i have files header.php and footer.php, i have created a new folder and file in the new folder and wanted to include the header and footer files from main. So the paths are:
main folder: example.com/header.php
new file: example.com/folder/new_file.php <- in this file i want to include the header.
I have tried:
<?php include "header.php"; ?>
<?php include "/header.php"; ?>
<?php include "./header.php"; ?>
<?php include "../header.php"; ?>
and none of them work. Any help would be appreciated thanks.

<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
it's will work .try may be its help in any of your files

include_once dirname(__DIR__).'/header.php';
This should work.

Thanks for all of your support. To answer my question thanks to all here are the answers.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
and
php include "../header.php";
DO work and i was able to include a file from main directory so thanks to david and code360.
second part of why the css file was not being read is because i had it like this in header.php file:
<link rel="stylesheet" href="css/bootstrap.css">
and missed the first "/"
Thanks again to code360

Related

How to include a file using a relative path?

I would like to include a file located in front/header/header.php from a front/user/login.php file.
How i can include header.php in login.php ? since it needs to step back from user folder and then enter to header folder.
i tried :
<?php include('/header/header.php'); ?>
<?php include('../header/header.php');?>
<?php include('../../header/header.php');?>
<?php include('header/./header.php');?>
<?php include('header/../header.php');?>
<?php include('front/header/./header.php');?>
<?php include('front/header/../header.php');?>
I think, you missed context of including path. Try to use:
include dirname(__FILE__) . '/../header/header.php';
It should helps.

php not executing in sub directories / php only working in root directory

I am refurbishing a website for a friend, e.g. making it more easy to program/maintain.
The server is running PHP 5.6 and in order to make life easier for me I wanted to uses php's include function to easily include stuff like the head or menu in every web page.
The file structure I use is index.php in the / directory and e.g. history.php in /pages. The files I am including e.g head.php lie in /php.
My problem is that in index.php
<?php include ('php/head.php'); ?>
perfectly executes and includes the designated file but in all sub directories such as /pages the same php code in history.php just doesn't execute at all leaving me with a blank line in the source code. I figured that this has to do with my PHP config or that said might be wrong, but I couldn't find the issue. I also tried calling the tech support of my web hosting provider but although they told me that everything should be working now I still get a beautiful blank line.
I've been searching for a solution to my problem for quite some days now, but I sadly haven't had any luck so far.
Hope the community can help
Thanks in advance
If you set the include_path to the full path then no matter in which sub-directory you have scripts they will always use the full path to the required file.
<?php
/* If you set this on every page the path will always be correct */
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/php/' );
include( 'functions.php' );
?>
<!doctype html>
<html>
<head>
<title>Include PHP files</title>
<?php
include( 'head.php' );
?>
<body>
<?php
include( 'menu.php' );
?>
<!--
regualar html,javascript etc
-->
<?php
include( 'footer.php' );
?>
</body>
</html>
Is it simply a file paths issue? If you're in a sub-map, then the path to head.php will be different:
<?php include ('../php/head.php'); ?>

Document root is not calling CSS

I do not know why this is not working. My header, footer, CSS and JS is not called when I am working in coffeeshop.php.
Mysite
I guess it is because it is not in the root folder. Therefore I made the include like this:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/header.php'; ?>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/navbar.php'; ?>
But is still not working?
Here is where I put the full URL:
$_SERVER['DOCUMENT_ROOT'] is return folder path
http://$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] it return server path
otherwise you can only use
<?php include '../resources/includes/header.php'; ?>
<?php include '../resources/includes/navbar.php'; ?>
try it

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.

How can i get link path if i want include another folder

I want to know that how can i give include path of another folder.
For example, i have a project named test folder. In test folder has one folder which is assets folder and index.php file. In assets folder has two folder which are include and xyz folder. In include folder has two file which are header.php and footer.php.In header.php file has three link home, product and customer. In xyz folder has product.php and customer.php file.
In index.php, product.php and customer.php i want to include header.php and footer.php.
But i can't right path.
<?php include('../../header.php');?>
...........
<?php include('../../footer.php');?>
Please help me, how can i give right path which if i click product link then header.php and footer.php will be load.
Try this :
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>
Ref: http://php.net/manual/en/function.dirname.php
If your Directory look something like this:
src
->includes
->header.php
->footer.php
web
->index.php
And you want to include the header and the footer in the index.php, do the following:
<?php
//index.php
include __DIR__.'/../src/includes/header.php';
echo "foobar";
include __DIR__.'/../src/includes/footer.php';
?>
http://php.net/manual/en/language.constants.predefined.php
Basically i have used like below
<?php include_once('../include/header.php');?>
<?php include_once('../include/footer.php');?>
echo realpath('../../header.php');
echo realpath('../../footer.php');
I think that if you can replace
../../header.php by ../include/header.php and
../../footer.php by ../include/footer.php.
then you will get accurate result.

Categories