1

As the title says, the include function is not working in a certain part of my code. As another section of the same file uses "include", I know that it is not an issue with this particular file, it is an issue with how I am trying to include it. I'm just a beginner in PHP so I can't figure out what I'm doing wrong, despite actual hours looking up different solutions and trying them.

It would take too much space to copy over the actual code, so I'm going to do a simpler version of it with just the needed information. The location of file1.php is:

$_SERVER['DOCUMENT_ROOT']."/TAO_3.1.0-RC3_build/taoDelivery/controller/file1.php"

The location of file2.php is:

$_SERVER['DOCUMENT_ROOT']."/dashboard/controllers/file2.php"

file1.php:

class ClassA {
   public function functionA()
   {
     //does its thing
   }

file2.php:

include("../../TAO_3.1.0-RC3_build/taoDelivery/controller/file1.php");

class ClassB {
   public function functionB()
   {
       //does its thing, and as testing I included a printout of all included files

       $included_files = get_included_files();
       foreach($included_files as $filename)
       {
           echo "$filename\n";
       }
   }

This does not work, the foreach loop that echoes all the included files successfully includes other files that are included, but not this one. The only different is that the other files are much closer in location, such as just one folder away, rather than multiple folders away.

I have tried

include("file1.php");

rather than the longer one that goes folder by folder, but it doesn't work either. I have also tried using require_once and require, but they just stop the rest of the code and the page does not fully load what it should (fatal error, which makes me more convinced that I'm missing something glaring in my syntax). Any help at all would be really really appreciated, thank you!!

Edit:

I have fixed the relative path, using ../../../ rather than the original two. This hasn't fixed the problem, for some reason. Using require() causes a fatal error and the rest of the code does not occur. I am (unfortunately and not ideally) working on a live website, so I will have to find out how to detect errors on this project in order to see what exact error require() brings up. Thank you for the helpful comments

Edit #2:

I found what the issue with including is, since I do not have access to error logs I dumped the error onto the screen using

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/log.txt');
error_reporting(E_ALL);

It resulted in printing out a fatal error:

Fatal error: Class 'tao_actions_CommonModule' not found in /var/www/html/taoplatform/TAO_3.1.0-RC3_build/taoDelivery/controller/file1.php on line 42

In conclusion, it is not the issue of the include function not working, it is a problem with the file I was trying to include, which is an entirely different problem. Thank you all for the help!

Colleen
  • 13
  • 3
  • 1
    If you know the file's path relative to DOCUMENT_ROOT, why not include it that way? `include $_SERVER['DOCUMENT_ROOT']."/TAO_3.1.0-RC3_build/taoDelivery/controller/file1.php";` – Michael Berkowski Jun 04 '18 at 18:49
  • 1
    What happens when you use the absolute path to file1.php? Also, what happens where you use require() instead of include()? Could it be that you are not displaying errors? – Mr Glass Jun 04 '18 at 18:51
  • But it would appear the relative path is `../../../` rather than two levels `../../` as you used because `dashboard/controllers` is 2 levels in from their common root at `$_SERVER['DOCUMENT_ROOT']`: One `../` goes up to `controllers/`, the next `../../` goes up to `dashboard/` so 3 `../../../` goes up to `$_SERVER['DOCUMENT_ROOT']` – Michael Berkowski Jun 04 '18 at 18:51
  • If the remaining code does not load and you've hit a fatal error (which `require` will cause while `include` issues a warning), we'd also recommend enabling error display, always while developing and testing code. At the top of your highest functioning include file, do `error_reporting(E_ALL); ini_set('display_errors', 1);` It won't tell you more than you already know in _this_ instance (that the file path is wrong) but future errors will be more avoidable. – Michael Berkowski Jun 04 '18 at 18:55
  • Michael, thank you, but for whatever reason neither of those things displays the file as being included. I corrected the relative path, and I tried the root method, but neither worked, unless the way I am echoing the included files is wrong. – Colleen Jun 04 '18 at 18:55
  • Mr Glass, using require() causes the page to not load everything as it should. I am (not ideally) working on a live website, so I have no idea how to enable error displaying. Thank you – Colleen Jun 04 '18 at 18:57
  • @Colleen, that error is the key to your issue. Please look at your PHP error log and post the error so we can see the reason your file1.php is not being loaded. – Mr Glass Jun 04 '18 at 19:03
  • Okay, if you cannot enable error display, you must get access to the web server's error logs, wherever they may be. – Michael Berkowski Jun 04 '18 at 19:18
  • Relative links going up the hierarchy may not work as you would expect (this may actually be relative to the current working directory (CWD) - not the file with the require line). Be more explicit and prefix with `__DIR__ `, so you are relative to the calling's file's location. As you are using files to include classes, I'd throw out the require lines and swap them for a PSR-4 compatible autoloader and corresponding hierarchy. – Progrock Jun 04 '18 at 19:31
  • MrGlass and Michael, I am attempting to find the error. Progrock, I tried the __DIR__ idea, and it stopped the rest of the code, just like with require(). It seems like if the line of code is accurate, it results in a fatal error and stops the code. Thank you all for your helping – Colleen Jun 04 '18 at 21:07
  • It could be that the file is not readable. Try assigning the path to a variable first. Then echo it, to make sure it's what you expect. Then try `var_dump(is_file($path));`, `var_dump(is_readable($path));`. e.g. `$path = __DIR__ . '/../../file1.php';` – Progrock Jun 04 '18 at 21:11
  • And check the path against the local file system. It might also help to get and share the octal permissions (if on a *nix): `stat -c "%a" /path/to/file.php` – Progrock Jun 05 '18 at 07:21

0 Answers0