0

For a while I've been testing projects on a server and then uploading it to the correct folders. Recently I decided to try using XAMPP for increased security and less hassle when testing; so I installed it, imported the database and copied the files to the htdocs folder.

Includes and requires seem to be working fine, but the one instance where it fails is when I try to access a parent folder, i.e. require "../file.php";. I tried playing around a bit with the include_path variable in php.ini, but nothing seems to be working.

  • I'm using Windows 10
  • XAMPP's version is 3.2.2

Any idea on what could be causing this, and how to fix it?

Many thanks in advance.


Edit:

The file structure goes like this:

folderA/folderB/folderC

What I try to do is...

  1. Have a file (fileA) in folderA that includes a file (fileC) in folderC
  2. The file (fileC) in folderC includes a file (fileB) in folderB

Where apache seems to fail is when including fileB from fileC.

The code is as follows:

in fileA.php: include "folderB/folderC/fileC.php";

in fileC.php: require "../fileB.php";

My include_path currently looks like this: include_path= ".;D:\xampp\htdocs\"

Mergan
  • 11
  • 3
  • 1
    whats the error?? add some code where you are struct – Sohel0415 Jan 22 '18 at 05:26
  • Warning: require(../file.php): failed to open stream: No such file or directory in D:\xampp\htdocs\parent\file.php on line 2 Fatal error: require(): Failed opening required '../main.php' (include_path='D:\xampp\htdocs\parent') in D:\xampp\htdocs\parent\file.php on line 2 – Mergan Jan 22 '18 at 05:29
  • 1
    `require __DIR__ . '/../file.php';` See http://php.net/manual/language.constants.predefined.php – Phil Jan 22 '18 at 05:29
  • it would be better if you add your folder structure – Sohel0415 Jan 22 '18 at 05:29
  • It is as follows: `outer/inner` I have the main.php file inside "inner", where it requires a file in "outer". – Mergan Jan 22 '18 at 05:31
  • @Phil I'm trying to see if I can find an answer that doesn't modify the code, since it works fine on the live server. It only doesn't work on XAMPP. – Mergan Jan 22 '18 at 05:33
  • it is working fine on my local environment on xampp :) – Sohel0415 Jan 22 '18 at 05:34
  • Crazy thought, is there a `D:\xampp\htdocs\main.php` file? `include_path` also typically includes `.`, as in the current working directory. Do you have a reason for removing it? – Phil Jan 22 '18 at 05:41
  • I've updated the question with a more "concise" description of the issue. – Mergan Jan 22 '18 at 05:42
  • What **exactly** do the `require` / `include` lines look like in `fileA` and `fileC`. Also, what **exactly** is your `include_path` set to? – Phil Jan 22 '18 at 05:43
  • 1
    FYI, relying on the environment configuration to correctly include files is bad and makes your application less portable. I strongly suggest changing your code to use absolute paths (via `__DIR__`) and / or setting the `include_path` based on an absolute path known to your app (again, `__DIR__`) – Phil Jan 22 '18 at 05:45
  • @Phil edited, thank you. – Mergan Jan 22 '18 at 05:52
  • Because your entry point is `fileA`, the `.` in the `include_path` refers to `folderA`. From that point on, any relative include path in **any included file** is relative to `folderA`. Are you beginning to see why this is not ideal? – Phil Jan 22 '18 at 05:53
  • I understand, thank you. I'll definitely append `__DIR__` to the paths; but how should the include_path be structured to include the files within? – Mergan Jan 22 '18 at 05:57
  • @Mergan you could set it to some logical point of entry. The `include_path` is a FIFO stack so set the best options first. Say you set it to `folderA`, you could then use `include 'folderB/folderC/fileC';` and `include 'folderB/fileB';` – Phil Jan 22 '18 at 06:04
  • See https://stackoverflow.com/questions/15752625/php-find-require-once-real-path/15752693#15752693 – Phil Jan 22 '18 at 06:05
  • I've set the `include_path` as `folderA` per your recommendation, and modified the require to `folderB/fileB`. It works like a charm, thank you. Please set it as the Answer so I can accept it. – Mergan Jan 22 '18 at 06:14

0 Answers0