Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making php includes work in a sub-directory

Ok, I am creating an admin interface for my custom blog at the url /admin.

Is it possible for me to be able to use the same includes (including autoload), as the root directory.

If possible, I would also like to be able to automatically correct the links in the navigation so that they go that index.php in / changes to ../index.php when accessed from /admin.

Thanks, Nico

like image 715
Nico Burns Avatar asked Jul 24 '09 22:07

Nico Burns


People also ask

Why include is not working in PHP?

You need to try the path of functions. php within the system and not its url. Do you have console access? If so just find out what directory is the file in and include it using the full path.

How to include a file to a PHP page?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


1 Answers

The best practice for this is to define a 'ABSOLUTE_PATH' constant that contains the directory that everything is located under. After that, you can simply copy and paste everything, because it is defining the 'full' path, which doesn't change from directory to directory.

Example

define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);

or

define("ABS_PATH", dirname(__FILE__));
// This defines the path as the directory the file is in.

Then at any point you can simply do this to include a file

include(ABS_PATH . "/path/to/file");
like image 79
Tyler Carter Avatar answered Sep 21 '22 07:09

Tyler Carter