Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php include_once path

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");
?>
like image 516
Frank Avatar asked Jan 31 '12 20:01

Frank


People also ask

What is Include_once 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.

What is __ DIR __ in PHP?

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.

What is the difference between include_once () and require_once ()?

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.

How do I join two PHP files?

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.


3 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 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.

like image 182
Cheery Avatar answered Nov 01 '22 01:11

Cheery


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");
?>
like image 31
Peter Krejci Avatar answered Oct 31 '22 23:10

Peter Krejci


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.

like image 1
Surreal Dreams Avatar answered Nov 01 '22 01:11

Surreal Dreams