Root Path Doesn't Work with PHP Include

PHP require does not work in root folder

This is a problem induced by using relative filepaths. Your relative filepath worked while the file blog.php was in the folder includes because with ../ you were going one directory up (to your root directory) and then back inside includes. But since you moved the file one directory up (to your root directory) you are going with ../ one directory up, outside of your root directory and then try to go into the folder includes, but the folder doesn't exist in the directory above of your root directory (well, if you have luck it does, but the file doesn't exist or it does but the wrong one).

To prevent this you should always use absolute filepaths, that way you can move blog.php everywhere as long as your file addbloguser.php stays in the same location.

A easy way to get the absolute filepath to your project is to define a constant which evaluates to dirname(__FILE__)) in a global file in your project's root directory. That way you can always use the constant to do things with files and prevent to modify files to correct relative filepaths if you change something.

E.g. global.php is inside your root directory, then you will just write this

define('IN_DIR', dirname(__FILE__)); //__FILE__ is a magic constant and includes the absolute filepath to the current file -> global.php

And if you now include in blog.php said file global.php (or the other way around) you can use for your filepaths the defined constant and it will look like this

require IN_DIR.'/path/to/file.php'

In this case that would be

require IN_DIR.'/includes/addbloguser.php'

php include working with URL, but not root path. Server configuration or coding syntax?

If the file is on the same server (which is probably the case 99.99% of the time) you shouldn't use a URL to include it. This is very inefficient because it will require an extra HTTP request by the server. And of course, what is outputted by the remote web server is what is included - not the PHP source code, but the parsed output. As well, anyone with access to the remote URL could inject code into your website. Instead you should be using the file system to access the files directly.

If you're including a file starting with a / then this is relative to the root file system, not the website root directory. That is why your third example wasn't working. For example, your web server could serve pages from /var/www/html, so to include a file from within that directory, you would need to either start it with /var/www/html/ or make the include path relative.

What I typically do is from a file located in the root of the application that is called on every request (e.g. configuration file):

set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);

Then in any other file, I can just include relative to the root directory for that app:

include 'path/to/subdirectory/file.php';

You just have to be aware of duplicate file names. For example, if you have:

/var/www/html
├── class
│   ├── controller
│   │   ├── include.php
│   │   ├── FruitController.php
├── include.php

And from within your FruitController.php file you include include.php, It will check the first path from get_include_path(), and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. So depending on the order you put in set_include_path(), it will either include /var/www/html/include.php or /var/www/html/class/controller/include.php

Another way to do this and also add explicit control to what you're including would be to put the following in a file in the app's root directory:

define('APP_ROOT', __DIR__);

Then in any other file just do:

include APP_ROOT . '/path/to/subdirectory/file.php';

How to PHP-Include from Root of Domain?

Use $_SERVER['DOCUMENT_ROOT']

Here's the server documentation.

The document root directory under which the current script is executing, as defined in the server's configuration file.

Together, it should look like this:

include $_SERVER['DOCUMENT_ROOT'] . "/include/footer.php";

php include doesn't work even inside the same folder

Try this

ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );

include './members/reg.php';

Possibly the include worked but caused a silenced error.

PHP include absolute path

You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :

<?php 
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
?>

php include() from server root?

You would actually need:

require_once("/home/codlife/public_html/test123/mvc/views/txt/index_nav_txt.php");

notice the edition of /home/codlife/public_html/

The initial / Takes you to the root of the server and your code is located inside /home/codlife/public_html/

How to set root folder for PHP include files

dirname(__FILE__) and __DIR__ both same and __DIR__ comes with PHP 5.3

These are used in order to indicate that the "path of the file where they called".


URL: http://localhost/test/a.php

DIR: --NIX
/var/www/test/a.php
--WIN
D:\lamp\www\test\a.php

// a.php's inside
<?php
echo __DIR__;

Gives you this on linux: /var/www/test

So, if you need a config parameter in all your project, just define it in your config.php and use it where you want both the file name that will be included.


./
config.php
index.php
header.php
footer.php
/lib
foo.php
/tmp
bar.php

./config.php
define('ROOT', __DIR__ .'/');

./index.php include_once(ROOT .'header.php'); ... include_once(ROOT .'footer.php');

i.e, using it in tmp dir

./tmp/bar.php include_once(ROOT .'lib/foo.php');

UPDATE

// config.php
<?php
define("ROOT", __DIR__ ."/");

So, we use this for index.php to include banner.php and banner.php is waiting in ./banners/banner.php;

// index.php and the very first line!
<?php
include_once("config.php");
?>
// some html stuff
// ...
<?php include_once(ROOT ."banners/banner.php"); ?>
// some more html stuff
// ...

So, you should include config.php first to where you need it.

I think, this is basic as far as needed...

UPDATE

So your problem is not PHP include system, but question, anyway... :)

If your image path is changing (so not fixed), you can do like this;

// config.php
define("ROOT", __DIR__ ."/");
define("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")
? "http://localhost/your_work_folder/"
: "http://your_site_name.com/"
);

// banner.php
<img src="<?php print HTTP; ?>images/banner.gif">


Related Topics



Leave a reply



Submit