Bing
Bing

Reputation: 3171

Execute one PHP file from within another - without modifying paths

I have two PHP files (NOT in the same directory) and one properties file, like so:

a.php
-----
<?php
echo "one";
include("/path/to/b.php");
echo "three";

and

b.php
-----
<?php
$num = file_get_content("b.properties");
echo $num;

where b.properties has two in it.

Since a.php and b.php are not in the same folder, the include in a.php causes the properties file not to load properly.

How do I go about fixing this? Note that I cannot modify b.php or b.properties or obviously this would be a trivial question.

Upvotes: 0

Views: 37

Answers (1)

Song Jia
Song Jia

Reputation: 36

PHP provides function chdir for changing PHP's curernt directory. I think you can change PHP's current directory to the folder of b.php just before include b.php and set it back after include.

a.php
-----
<?php
echo "one";
chdir('/path/to');
include("b.php");
chdir(__DIR__);
echo "three";

Upvotes: 2

Related Questions