Yuantao
Yuantao

Reputation: 2872

include and include_once issue in PHP

I currently working on a PHP project. I copy the project file to my local box. It runs fine except one thing.

Here is the folder hierarchy:

root/index.php
root/event/admin/list.php 
root/event/admin/functions.php

In the index.php, there is a line:

<?php include ("event/admin/list.php"); ?>

which should include the list.php

However in the list.php, there is a line:

<?php include_once "event/admin/functions.php";?>

Since the list.php is not in the root directory, event/admin/functions.php did not get call and my local index.php fail to load this part. But the production is working fine.

Does anyone know what happened? Is that a way to setup include/include_once always use ROOT directory without using something like $_SERVER["DOCUMENT_ROOT"]? Thanks a lot.

Upvotes: 1

Views: 645

Answers (4)

David W
David W

Reputation: 945

Compare the include path configuration between the two machines.

Upvotes: 0

Anonymous
Anonymous

Reputation: 553

It is a good idea to use $_SERVER['DOCUMENT_ROOT'], in my opinion. You can do so like this:

include_once($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.php");

However, try replacing he code in list.php with this:

<?php include_once "../../event/admin/functions.php";?>

This is a known issue relating to relative paths. Thus, DOCUMENT_ROOT is preferable. Alternatively, you can edit include_path.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Set the include path to whatever is useful for your probject.

Upvotes: 1

Brad
Brad

Reputation: 163262

This is a common issue related to relative paths. You can either include your files from an absolute path, or modify your include_path in php.ini to use your doc root, and specify files relative to there.

Upvotes: 0

Related Questions