Php include_once path

I am trying to find a way to start the path of include_once(); from the source directory, and then find the path, for example. instead of ../../../path/to/file I would have /path/to/file . If I do this /path/to/file , it does not talk about this file or directory, here is my direct code

 <?php include_once("/assets/page_assets.php"); ?> 
+7
source share
4 answers

If you have an Apache server

 <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/assets/page_assets.php"); ?> 

"/assets/page_assets.php" means from the root of the disk , not from the root folder of the server. If you are talking about some other beginning directory , then define the physical path (the path in the server file system, not the web path) to it as a separate constant / variable and add it to the included path, as shown above.

+12
source

You can specify the include path explicitly:

 <?php ini_set('include_path',ini_get('include_path').':../../../:'); ?> 

But, as Cheery said in a comment, you should include your file without a slash:

 <?php include_once("assets/page_assets.php"); ?> 
+2
source

To do this, you must use the path from the root. In some cases, this may look like /var/www/mysite.com/assets/page_assets.php . One way to find this way is to use __FILE__ (this 2 underlines both the front and the back). If you repeat this from a file, it will show you the full path. You must use this to set the correct full path.

+1
source

Run the script with chdir($_SERVER['DOCUMENT_ROOT']);

Now you can call include("path/to/file.php"); and he will start looking from the website.

0
source

All Articles