How to include a file using a relative path? - php

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.

Related

Why does my included/required file with variables stop working when I upload everything?

My index.php gets all parts for the website seperated and it looks like this:
<?php
include 'var1.php';
?>
<?php
include 'var2.php';
?>
<?php
include 'https://www.example.de/city/head.php';
?>
<?php
include 'https://www.example.de/city/header.php';
?>
<?php
include 'https://www.example.de/city/content.php';
?>
<?php
include 'https://www.example.de/city/footer.php';
?>
<?php
include 'https://www.example.de/city/bottom.php';
?>
The main parts like head, header etc. are always the same but for each city I need different variables and thatswhy I got a folder (for example 'Berlin') with only index.php, var1.php and var2.php in it.
This is working fine on my computer but as soon as I upload it to get it online, the variables from var1.php and var2.php are not included anymore.
Can anyone see something wrong here?
Fixed it with:
include($_SERVER['DOCUMENT_ROOT'].'/city/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 from main folder to a file in different folder

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

include not work (PHP)

i have strange problem with include commnad
my files structure
/index.php
/files/init.php
/files/db.php
and sources of this files
index.php
<?
include ('files/init.php');
?>
init.php
<?
include ("db.php");
?>
Source from db.php is not executing.. NOW if i rename file db.php to db2.php and rename it in init.php NOW IT WORKS
there is some php cache or what ? i dont understand it.
conflict filename DB.php from PEAR.
DB.php from PEAR is included instead of your "db.php"..
So use filename other than "db.php".
EDIT or use include with DIR macro
try this
<?php
include ("db.php");
?>
or using ./
<?php
include ("./db.php");
?>
The included files are resolved according to the entry point (here index.php).
Moreover, require is better is the file MUST exist.
index.php
<?php require 'files/init.php'; ?>
files/init.php
<?php require 'files/db.php'; ?>
Update 1: As suggested by #Darsstar, here is an example using __DIR__:
files/init.php
<?php require __DIR__.'/db.php'; ?>
Update 2: Maybe you should consider PSR-0 or PSR-4 to deal with file inclusion.

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