tluebke
tluebke

Reputation: 31

How to support variable in include()?

I have a simple php file with an include command which does not work in my case.

$rootfolder = 'www.example.com/new/';
include $rootfolder.'_header.php';

My _header.php file is in www.example.com/new/, but is not included. I have seen instructions, telling that this should work.

When I check my include path with

echo $rootfolder.'_header.php';

it works perfectly and directs to the correct file: "www.example.com/new/_header.php"

But why isn't it working for me in the include line?

Can you help me? Thank you in advance.

Upvotes: 0

Views: 70

Answers (2)

Ramiz Web Dev
Ramiz Web Dev

Reputation: 621

there is kinds of technique to access file form anywhere i give some of that

if you want access from other folder then do this below

below folder hierarchy see care fully

-root

  • css
  • include
    • _header.php
  • js
  • admin
    • manager.php
  • index.php

if you include _header.php in index.php then

include "include/_header.php";

if you include _header.php in manager.php from admin folder then

include "../include/_header.php";

hear tow .(dot) indicate go back one step in hierarchy

if not any one you ans then please ask again in comment

Upvotes: 0

AnatPort
AnatPort

Reputation: 748

You are using the wrong root. try figuring out the absolute path to the file using realpath:

$rootfolder = realpath($_SERVER["DOCUMENT_ROOT"])."/new/";
include $rootfolder.'_header.php';

Or use __FILE__ to echo the file path from _header.php to see whats it absolute path

$path = dirname(__FILE__);
echo "$path";

Upvotes: 1

Related Questions