Reputation: 65
Is it possible to require files (using php require) from the directory where my website is placed? For example, my website is in the directory mywebsite which is in the root directory. There is another directory there. Can I require files from this another directory?
Upvotes: 2
Views: 2405
Reputation: 5708
I have been using
require_once $_SERVER['DOCUMENT_ROOT'] . '/myfile.php';
Upvotes: 1
Reputation: 1527
It should be no problem to require from an arbitrary existing and readable directory.
Image you have:
/
--folder1
--folder2
and in folder1 is your index.php and in folder2 is your to_require.php
Then you could write:
require('../folder2/to_require.php')
That's because you can go up in your directory tree with ..
Upvotes: 0
Reputation: 163622
Sure, you can require files from anywhere that has the appropriate permissions.
This requires the file from the current directory (NOT always where the current PHP script is, so be careful of that):
require("whatever.php");
This will require whatever.php
from somefolder
which is in the current directory.
require("somefolder/whatever.php");
Finally, you can give an absolute path like this:
require("/var/www/includes/whatever.php");
Require from parent directory:
require("../includes/watherver.php");
It doesn't matter really where you get it from, provided you have the permissions set correctly, and PHP is configured in such a way to allow you to do so.
Upvotes: 2