Jump to content

Recommended Posts

How to include root files from subdirectory in php?

 

I have tried include '../configure.php'; but got error.

 

Also tried include($DOCUMENT_ROOT."../configure.php"); but same result.

 

For example, I have "root/admin" directory, and can't connect (from "/admin" dir)  to the file configure.php which is in root. There are also other files to include.

Link to comment
https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/
Share on other sites

Not sure what your problem is. Using '../' should let you access files that are one level up from the file being executed.

 

The error you received should give you the FULL path to where the server is looking for the file. What WAS the error?

 

Assuming the below diagram is the structure in question, using '../configure.php' in the index.php file should work.

 

[ROOT]

|

|-- configure.php

|

|-[admin]

    |

    |-- index.php

Using '../' should let you access files that are one level up from the file being executed.

From the first file that was executed (generally). Subtle difference. Moral is to always use absolute paths whenever possible, either with the DOCUMENT_ROOT or the __FILE/DIR__ constants.

require_once($_SERVER["DOCUMENT_ROOT"] . "/configure.php");

Using '../' should let you access files that are one level up from the file being executed.

From the first file that was executed (generally). Subtle difference.

 

You're right. I considered that for 1/2 a second, but then second guessed myself and thought it didn't apply to this situation. But, now that I consider it, I don't know what I was thinking (or not thinking). That could very well be the problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.