Why requires and includes can not add files that are in directories above the call file?

0

I noticed that require and includes can not include files that are above the directory of the calling file. Why does this occur?

Example:

This works:

<?php require_once('inc/configs.php'); ?>

This does not work:

<?php require_once('../template/header-home.php'); ?>

That is, if you have to add a file that is above the directory of the calling file, it gives error saying that the file was not found.

    
asked by anonymous 10.01.2016 / 17:46

2 answers

0

You can access yes! just know the simple technique.

Please note: the file I'll need is:

C:\xampp\htdocs\slug.php

The file I'm going to use to include "slug.php" is:

C:\xampp\htdocs\uploads\updoc.php

In the file "updoc.php" goes the simple code:

<?php
include_once("\..\slug.php");

It's very simple to access files wherever they are.

If the file "slug.php" is in the "xampp" folder next to the "aploads" folder

In Windows it would be:

<?php
include_once("\..\xampp\slug.php");

I hope I have helped!

    
10.01.2016 / 20:53
-1

Try using the constant __DIR__ :

require_once(__DIR__ . '/../template/header-home.php');
    
10.01.2016 / 17:51