Question

how to include the same relative php in different folder depth php?
for some reason , I need to add a www/subam/index.php in my zencart website , and this php need to the www/includes/application_top.php , but if I require(../includes/application_top.php) ,it will go wrong . how to do?

<?php 
/* index.php */
require('includes/application_top.php');

/* subam/index.php */
// this will be wrong
require('../includes/application_top.php');
?>
Was it helpful?

Solution 2

You'll need to set your include path

set_include_path(get_include_path() . DIRECTORY_SEPARATOR . '..');

OTHER TIPS

always use physical full path then you won't have any problem:

php 5.3:

require(__DIR__ . '/lib/file.php');

if you need to go back from where you are use ../:

require(__DIR__ . '/../lib/file.php');

php 5.2 and lower: replace __DIR__ with dirname(__FILE__)

in your example:

/* index.php */
require(__DIR__ .  '/includes/application_top.php');

/* subam/index.php */
require(__DIR__ .  '/../includes/application_top.php');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top