Home » Resources » Programming » PHP » Solution: Using Include or Require in PHP but Can’t Find File

Solution: Using Include or Require in PHP but Can’t Find File

No Comments PHP Print Print


The situation is you are trying to use include or require to load a file from a folder in your site, when the file you are calling include or require from is inside a different folder in the site and NOT the document root.

The issue is, when you use include outside of your document root, it considers that folder as the document root, so you are forced to give it the real root directory of your website on the server.

Your File:

website.com/directory/script.php

Your Code in that file:

require_once( "/directory2/filename.php");

or

include_once( "/directory2/filename.php");

If properly configured, you can use this PHP Server Key/Variable.

$_SERVER["DOCUMENT_ROOT"]

So a good Example would be the following:

include_once( $_SERVER["DOCUMENT_ROOT"] . "/directory2/filename.php");

or

include_once( $_SERVER["DOCUMENT_ROOT"] . "/directory2/filename.php");

In the case that directory is the folder just after the root of your site.

About the author

geilt has written 35 articles for Esotech Inc.

Archives
May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  
Contact Us

888-646-9653

info@esotech.org

Latest Tweets