0

I recently moved to a cloud server from a dedicated server environment and I'm having trouble with root relative paths inside of a php include. When I include the entire URL, the php include works perfectly. But it does not work when I use the root relative path in a subdirectory. Ideas?

Working with URL

<?php include 'http://website.com/assets/includes/nav.php';?>

file location http://website.com/folder/page

Working, but only in root index.php file. Will not work in subdirectory

<?php include 'assets/includes/nav.php';?>

file location http://website.com/index

NOT Working with root relative path

<?php include '/assets/includes/nav.php';?> 

file location http://website.com/folder/page

1
  • / is the root of the server, not the root of the web path and include with http means your calling it via the weberver so any code will be executed, but usable. The first one should work in any directory as its an absolute path, but you usually dont call it va the server Commented Feb 10, 2018 at 1:41

1 Answer 1

2

If the file is on the same server (which is probably the case 99.99% of the time) you shouldn't use a URL to include it. This is very inefficient because it will require an extra HTTP request by the server. And of course, what is outputted by the remote web server is what is included - not the PHP source code, but the parsed output. As well, anyone with access to the remote URL could inject code into your website. Instead you should be using the file system to access the files directly.

If you're including a file starting with a / then this is relative to the root file system, not the website root directory. That is why your third example wasn't working. For example, your web server could serve pages from /var/www/html, so to include a file from within that directory, you would need to either start it with /var/www/html/ or make the include path relative.

What I typically do is from a file located in the root of the application that is called on every request (e.g. configuration file):

set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);

Then in any other file, I can just include relative to the root directory for that app:

include 'path/to/subdirectory/file.php';

You just have to be aware of duplicate file names. For example, if you have:

/var/www/html
├── class
│   ├── controller
│   │   ├── include.php
│   │   ├── FruitController.php
├── include.php

And from within your FruitController.php file you include include.php, It will check the first path from get_include_path(), and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. So depending on the order you put in set_include_path(), it will either include /var/www/html/include.php or /var/www/html/class/controller/include.php

Another way to do this and also add explicit control to what you're including would be to put the following in a file in the app's root directory:

define('APP_ROOT', __DIR__);

Then in any other file just do:

include APP_ROOT . '/path/to/subdirectory/file.php';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was it and I really appreciate the detailed answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.