-2

I have language file (lang.php) that located in the root folder.

The problem is that i have 2 files that call function print_pm_texts and each of them located in a different folder

  • 1 in the root and

  • the second in the "/func".

because of that i can't use "include "lang.php". - When the file in the root folder call it than all ok, but the second file doesn't find it. when i change it to: "include ../lang.php than the root file doesn't find the file and the second one does.

I also tried to write: include "./lang.php" but it doesn't work either...

How can it be fix?

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
Roi
  • 47
  • 1
  • 7
  • Use a different `include()` in each file? Use the fully qualified path to the included file? – David Sep 07 '17 at 14:17
  • Are you able to 'include' each of the distinct file paths in each file? – aJetHorn Sep 07 '17 at 14:19
  • Look into the `$_SERVER['DOCUMENT_ROOT']` ([src](http://php.net/manual/en/reserved.variables.server.php)) variable, and use that to create the link to the file. – aynber Sep 07 '17 at 14:22
  • [have you tried googling using include with a relative path](https://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code)? [here is another one, probably more relevant to your scenario](https://stackoverflow.com/questions/17407664/php-include-relative-path) – Tiffany Sep 07 '17 at 14:32
  • Possible duplicate of [Are PHP include paths relative to the file or the calling code?](https://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code) – Ikari Sep 07 '17 at 14:33
  • A recent answer I posted - https://stackoverflow.com/questions/46067893/how-to-structure-the-php-files/46068112#46068112 – Nigel Ren Sep 07 '17 at 14:37
  • I had a few problems with `$_SERVER['DOCUMENT_ROOT']` when testing. As it is usually set by a browser request, it wasn't being set when I ran it through the CLI in phpunit. – Nigel Ren Sep 07 '17 at 14:38
  • 1
    You should always, always, always make paths relative to `__DIR__`. – NikiC Sep 07 '17 at 14:49

2 Answers2

0

You could try spl_autoload_register and absolute path like

require_once 'C:\Apache\htdocs\myProject\lang.php';

Max
  • 160
  • 1
  • 11
0

According to you, your directory structure is like below.

- Root
    - lang.php
    - firstfile.php
    -func/
       -secondfile.php

Now if you want to include lang.php in firstfile.php use below line.

include "lang.php";

If You want to include lang.php in secondfile.php, then use below line.

include "../lang.php";
Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
  • your wrote the directory structure right. I have another file that contain function. One time the first file call the function and on another time the second file call the function. It's mean that "include" need to fit both ways – Roi Sep 07 '17 at 14:40
  • you wrote the structure good. you just need to add new folder: "ajax/". both file inside "ajax" and "firstfile.php" call to file "secondfile.php". "secondfile.php" call to "land.php" which located in the root – Roi Sep 07 '17 at 14:47
  • is `ajax` folder is in `func/` folder or in `root` folder ? – Hamza Zafeer Sep 07 '17 at 14:48