AlotJai
AlotJai

Reputation: 147

How to use and read php file inside different folder in PHP ?

I want my .php file categorize inside the folder. I have this root directory that contains folder and php file as below .

core 
   initial.php // database connection
css
   style.css
js 
   js.css
apply
   index.php
   apply.php
templates
   head.php
   footer.php
index.php

My root index.php, I just include the file as usual. Note* - head.php and footer.php contains HTML file.

<?php
    require_once 'core/initial.php';
    include 'templates/head.php';

    echo '<a href='apply/index.php'>Apply</a>';    

    include 'templates/footer.php';
?>

But my problem is, my index.php file inside apply folder cannot call initial.php.

Warning: require_once(core/initial.php): failed to open stream: No such file or directory in C:\xampp\htdocs\movement\production\index.php on line 2

Fatal error: require_once(): Failed opening required 'core/initial.php' (include_path='\xampp\php\PEAR') in root\test\apply\index.php on line 2

My apply/index.php code

<?php
    require_once '../core/initial.php';

    include '../templates/overall/header.php';

    // PHP code here

    include '../templates/overall/footer.php';

The reason is I don't want my .php file all located in root directory. I want each php file except index.php in their own folder.

How can I do that ? Can someone help me on this problem ?

Thanks.

Upvotes: 0

Views: 5220

Answers (4)

BeetleJuice
BeetleJuice

Reputation: 40896

In initial.php or some other helper file, do:

/**  List of all dirs you wish to include from, relative to the document root  */
const INCLUDE_DIRS = [
  '/',
  '/core/',
  '/apply/',
  '/templates/'
];

function getPath($filename){
  $rootDir = $_SERVER['DOCUMENT_ROOT'];
  $path = null;

  foreach(INCLUDE_DIRS as $dir){
    if(file_exists($path =$rootDir . $dir . $filename)) return $path;
  }      
  throw new Exception("Could not find $filename in any of the INCLUDE_DIRS directories");
}

Now you only have to get a single include right; the file that holds the function. Always include it using a full path so it will work from anywhere:

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php'

You can go even further and make use of the auto_prepend_file directive which tells PHP to always execute that file before running the regular script. That's a great file to have the function above, and PHP will include it for you automatically.

Anyway, once the file with getPath() is included, you never have to worry about using the right path again. Just make sure all include directories are listed in the INCLUDE_DIRS constant and include/require your files like this:

require_once getPath('footer.php'); //getPath will scan INCLUDE_DIRS and find it

Note an important limitation of this approach is that if you have two files by the same name, it won't work because the first one found will be included. In that case, just be sure to always use the full path when including/requiring starting with $_SERVER['DOCUMENT_ROOT']

Upvotes: 2

Nikhil Parmar
Nikhil Parmar

Reputation: 499

Did you tried like this?

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php';
include $_SERVER['DOCUMENT_ROOT'] . '/templates/head.php';

Upvotes: 2

Meloman
Meloman

Reputation: 3712

Relative path doesn't work with Windows... On linux it does.

The best is to define a constant in a root file called defines.php and call it in index.php with require_once.

defines.php

DEFINE('_APP_ROOT_','c:\xampp\...');

After that, you will use this absolute path in all require and includes like it :

apply/index.php

require_once _APP_ROOT_ . 'core/initial.php';
include      _APP_ROOT_ . 'templates/overall/header.php';

You can find more info here : PHP - include() or require() with relative paths won't work on windows, even when appending __DIR__

Upvotes: 0

Carlos Alves Jorge
Carlos Alves Jorge

Reputation: 1985

Why not just always link based on root like:

    require_once '/core/initial.php';
    include '/templates/head.php';

Upvotes: 0

Related Questions