0
votes

I am having trouble understanding how PHP chooses to compile or how including files within files works.

Here's my file structure:enter image description here

I require secrets.php inside head.php. I require head.php inside index.php.

head.php

require "../environment/secrets.php";

index.php

require "php/head.php";

I get an error:

Warning: require_once(../environment/secrets.php): failed to open stream: No such file or directory in /home/ubuntu/workspace/Cally Dai/php/head.php on line 2 Call Stack: 0.0002 234760 1. {main}() /home/ubuntu/workspace/Cally Dai/index.php:0 0.0007 236632 2. require_once('/home/ubuntu/workspace/Cally Dai/php/head.php') /home/ubuntu/workspace/Cally Dai/index.php:5 Fatal error: require_once(): Failed opening required '../environment/secrets.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/ubuntu/workspace/Cally Dai/php/head.php on line 2 Call Stack: 0.0002 234760 1. {main}() /home/ubuntu/workspace/Cally Dai/index.php:0 0.0007 236632 2. require_once('/home/ubuntu/workspace/Cally Dai/php/head.php') /home/ubuntu/workspace/Cally Dai/index.php:5

where am I going wrong?

2
This might be a longshot, but have you already tried setting the require "../environment/secrets.php"; relative to index.php? It's worth a shot. require "environment/secrets.php";Timothy Bomer
@TimothyBomer That makes everything in the root directory work but other files, for example in the project folder, now start failing. It seems the file is being required relative to the file that is requesting the required file...but that shouldn't be the behavior correct?Govind Rai
Try changing: require "../environment/secrets.php"; to require (realpath(dirname(__FILE__)."/environment/secrets.php"));Timothy Bomer

2 Answers

1
votes

According to me, I feel that the path must be from root in ubuntu. Try your code with,
require_once("/home/ubuntu/workspace/Cally Dai/php/head.php")

0
votes

When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.

PHP will often report that it is unable to find the third file, but why? Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory. In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B. So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect. This is a very important point to understand about the difference between PHP and other languages like C/C++.

One solution is to use dirname(__FILE__):

Use dirname(__FILE__) – The __FILE__ constant contains the full path and filename of the script that it is used in. The function dirname() removes the file name from the path, giving us the absolute path of the directory the file is in regardless of which script included it. Using this gives us the option of using relative paths just as we would with any other language, like C/C++. We would prefix all our relative path like this:

include(dirname(__FILE__) . "/dir/script_name.php");

Source: http://yagudaev.com/posts/resolving-php-relative-path-problem/