Why do requires and includes fail to add files that are in directories above the call file?

Asked

Viewed 67 times

0

I realized that require and includes cannot include files that are above the call file directory. Because this occurs?

Example:

It works:

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

That doesn’t work:

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

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

  • 1

    You are not knowing how to interpret the error you are receiving. If the path was right, it would work. Post the error message and its folder and file structure so we can help you better.

  • 3

    PHP is allowed access to the path?

  • 1

    It is probably the directory permission. I do this often, usually.

2 answers

0

You can access it! just know the simple technique.

Note: What I’ll need is:

C:\xampp\htdocs\slug.php

The file I’ll use to include "slug.php" is:

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

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

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

it’s very simple to access files wherever he is.

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

In Windows would be:

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

I hope I’ve helped!

-1

Try to use the constant __DIR__:

require_once(__DIR__ . '/../template/header-home.php');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.