I am trying to find a way to make the include_once();
path start from the beginning directory and then find the path e.g. instead of ../../../path/to/file
I would have /path/to/file
. If I do do that /path/to/file
it says no such file or directory, here is my direct code
<?php
include_once("/assets/page_assets.php");
?>
The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.
The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue. There is also an include_once function which includes a file once.
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.
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 physical path (path in the file system of the server, not the web path) to it as a separate constant/variable and prepend it to the included path as shown above.
You can specify include path explicitly:
<?php ini_set('include_path',ini_get('include_path').':../../../:'); ?>
But as Cheery mentioned in comment, you must include your file without leading slash:
<?php
include_once("assets/page_assets.php");
?>
To do this you must use the path from the root. In some cases this might look like /var/www/mysite.com/assets/page_assets.php
. One way to find that path is to use __FILE__
(That's 2 underscores both in front and behind). If you echo that from a file, it will show you the complete path. You should be able to use that to set the correct full path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With