PHP include_path issue

Hi, I have a problem with php include_path, all php includes are failing, something like:

include(‘Helper.php’);

include(‘Enchance.php’);

// Third party functions

include(‘misc.php’);

are all failing because php search in the wrong path and can’t find the files. Sure, adding absolute path works but I can’t do this on all files. Any suggestions ?

Just to be clear, it's not about my code, I've set up a web server on a linode and then installed wordpress, many plugins and themes give errors because of this. How to set include_path in php.ini (or maybe other settings) to not give errors when working with relative paths ?

Thank you.

2 Replies

No PHP script should ever require includepath to be set in php.ini… If they do, it's bad code. You can use a relative path (like "mysiteincludes/functions/foo.php") if needed.

I would recommend using a constant with an absolute base path, like so:

(in your front-end files)

// index.php
define('ROOT_PATH', dirname(__FILE__));

// [...]

include ROOT_PATH . '/includes/mydirectory/myfile.php';

(deeper inside your application, front-end file in a subdirectory)

// subdirectory/index.php
define('ROOT_PATH', dirname(dirname(__FILE__)));

// [...]

include ROOT_PATH . '/includes/mydirectory/myfile.php';

You can also use that deeper in your code to maintain clear and understandable include paths, as well. Since all includes would be from the perspective of your forward-facing files, it'd mimic the behavior of using the include_path while still allowing yourself adaptability. To maintain your root path, just adjust your constant delcaration (and instead of using .. in the root path, use dirname to go up a directory in the string)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct