-1

index.php:

<?php require_once("include/header.php") ?>

header.php:

<?php require_once("../../include/config.php") ?>

I have php files and I need to access them using the included file so I need to use something like <?php require_once("../../include/config.php") ?> that will work with relative paths to this header.php, not to the file which includes my header.

misticos
  • 668
  • 4
  • 20
  • 6
    To answer this question, we would need more information about the structure of your files and folders. `../` means that you go up one folder in the hierarchy. – Qirel Nov 15 '18 at 18:06

2 Answers2

1

All include / require / require_once statements using relative files are relative to the PHP process' current working directory which depends on how PHP runs. You can get the current working directory using the getcwd() function and change it at runtime using chdir().

If you want to include a file relative to the current file regardless of the current directory, you can do something like:

<?php require_once(realpath(__DIR__) . "/../../include/config.php") ?>

Note the use of __DIR__ which is a magic constant that always represents the directory of file it is written in (not the including file).

shevron
  • 3,115
  • 2
  • 20
  • 32
0

Try this <?php require_once("./include/header.php") ?>