0

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.

5
  • 1
    Can you a.php to the same folder as b.php? I'm not sure if I understand well what your problem is. When you write a question about an error, always include details of the error. Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented Oct 23, 2015 at 0:29
  • You can't do what you want without modifying the path specified in file_get_contents in b.php. This is the problem with using relative paths. The arbitrary constraints you're creating around this problem lead me to believe that it's contrived and most likely an X/Y Problem which aren't very welcome on StackOverflow. Commented Oct 23, 2015 at 0:55
  • You could add the directory of b.php to the php include directive. Then the path wouldn't matter so much, although I don't understand why you can't edit the path... php.net/manual/en/function.set-include-path.php Commented Oct 23, 2015 at 1:22
  • a.php is a web-served page, while b.php is not in the webroot and needs to stay that way. The only way it can be hit is if a.php calls it. Commented Oct 23, 2015 at 2:01
  • Not in the web root but is the path relative to the web root consistent? Try using $_SERVER['DOCUMENT_ROOT'] . "../path/to/b.properties"); Commented Oct 23, 2015 at 3:33

1 Answer 1

2

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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.