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?
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.
I have been using
require_once $_SERVER['DOCUMENT_ROOT'] . '/myfile.php';
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 ..
Related
I have a small PHP/MySQL project I would like to upload to our subdomain. The project has an includes/ folder that contains some PHP files that have information about the database name, username, password and login function.
How can I make the files of this directory readable by the website (so when someone comes to the website, they can log in and do other stuff) but not accessible to the public? I can use a file downloader to download the content of the folder which is something I want to block.
Is the solution using a .htaccess file?
EDIT:
Thank you all for the answer. After some reading, I switched my folder structure to be like this:
includes/
- initiate.php
- login.inc.php
- functions.inc.php
public/
- index.php
- login.php
templates/
- header.php
- footer.php
I'm now having issues setting up relative and absolute path constants though
The initiate.php has my constant variables:
define('INITIATE_FOLDER', dirname(__FILE__));
define('ROOT_FOLDER', dirname(INITIATE_FOLDER));
define('TEMPLATES', ROOT_FOLDER . '/templates');
define('INCLUDES', ROOT_FOLDER . '/includes');
define('WWW_ROOT', ROOT_FOLDER . '/public');
When I echo out the constants, I get the followings:
echo INITIATE_FOLDER; C:\wamp64\www\project\includes
echo ROOT_FOLDER; C:\wamp64\www\project
echo INCLUDES; C:\wamp64\www\project/includes
echo TEMPLATES; C:\wamp64\www\project/templates
echo WWW_ROOT; C:\wamp64\www\project/public
Can you please tell me what I'm doing wrong and how to correct it?
If your server setup is correct, no PHP file will get downlaoded, only executed.
Basically, you have PHP extension installed nad if the file starts with <?php then it will be executable.
As others have said, all content between <?php ?> tags will be removed from the page before it's served by your server, so long as your file ends in .php.
If you are trying to keep a non-php file from being served, your best bet is to put your includes folder where it is not publicly available.
Generally, when you FTP into your server, the layout is something like this:
www/
public_html/
... etc, other folders
The files you want to make publicly available should go inside of the public_html/www folder (www is usually just a shortcut/symlink for public_html).
You includes directory should go next to the public_html folder, rather than within it.
www/
public_html/
includes/
... etc, other folders
Then, in the files where you were including those files, include them from the new location.
<?php
require_once "includes/databaseSettings.php";
becomes
<?php
require_once "../includes/databaseSettings.php";
Now your files are outside of the directory being served by your HTTP server, but still available to be included in the rest of your code.
This has usually been my experience, but can vary from vendor to vendor. If, when you FTP into your server, you don't see a www or public_html folder, try navigating up one directory.
I want to use __dir__.
However, I can't find any good tutorial on how to set it up. I have my htdocs in Dropbox.
Does it work something like this?
define(__DIR___, 'd:documents/dropbox/yolo/swag/htdocs/myproject/test/newtest/
testphp/test_new/testincludes/1/new_folder/')
That is the directory where my project is located and it has sub folders. I want to include a file into another file that is in the parent folder.
Should I then just type:
include'__DIR__/warlock.php';
Or do I have to type something like this?
include '___DIR__/wow/newb/guidesfornabz/classes/casters/warlock.php';
You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.
Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.
project
├── inc
│ ├── file1.php
│ └── file2.php
└── index.php
If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:
<?php
include __DIR__ . "/file2.php";
To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:
<?php
include __DIR__ . "/../warlock.php";
I've been looking to use an executed in apache's root htdocs _DIR_ variable and be able to include other scripts containing sensitive data such as database login credentials sitting outside it. I struggled a bit trying different options but the below is working really well.
Firstly, in my apache virtual host config I set/include a full linux path to apache's htdocs (you can add more paths by appending at the end :/path/to/folder/):
php_value include_path ".:/var/www/mywebsite.ext/uat/htdocs"
Then in .htacess stored in apache's htdocs root (and git repo):
php_value auto_prepend_file "globals.php"
Both of the above can be set in .htacess although it wouldn't work well for multiple environments suchas as DEV, UAT, PRODUCTION in particular when using git repo.
In my globals.php file inside apache's htdocs I have defined a variable called DIR that's globally used by htdocs php scripts:
define('DIR', __DIR__);
Then in each file am was now able to include/require necessary files dynamically:
require_once(DIR.'/file_folder_inside/apache's_htdocs/some-file.php');
The DIR variable would always resolve to /var/www/mywebsite.ext/uat/htdocs no matter where in the tree I call it in the above example producing
/var/www/mywebsite.ext/uat/htdocs/file_folder_inside/apache's_htdocs/some-file.php.
Now, I was looking to access a php file that's sitting outside my apache's htdocs root folder which is also easily achievable by using:
require_once(DIR.'/../apache's_htdocs_parent_folder/some-file-stored-outside-htdocs-eg-snesitive-credentials.php');
This is an example of how to use __DIR__ and go to the parent directory in different PHP versions, so if you can recognize which one is old and which one is new:
For PHP < 5.3 use:
$upOne = realpath(dirname(__FILE__) . '/..');
In PHP 5.3 to 5.6 use:
$upOne = realpath(__DIR__ . '/..');
In PHP >= 7.0 use:
$upOne = dirname(__DIR__, 1);
here is my project structure:
Web root/document root:
/var/www/ all the website files in the the 'www' folder
I want to use require or include a file inside my folder OUTSIDE the web root/document root:
/var/extra-files
how can i get a file inside the extra-files folder using require or include?
require "OUTSIDE DOCUMENT ROOT/extra-files
thanks
First make sure apache as permission to read the files on /var/extra-files, then you can use:
require "/var/extra-files/file.php";
You may want to read Difference between require, include and require_once?
Hmm wow... no way was it that easy. I just used this:
/var/extra-files/myfile.php
and it worked.
This is a big confusing but I'll try to be as clear as possible.
I am using the Starter Site PHP Template in Web Matrix for an assessmnent, now when I move several files such as the files containing the database details, header, footer ect.. into the admin file I am having an issue with my index.php and page.php.
These files use require_once() to include several files within the admin folder. However in the admin folder the files being required have require_once tags again within that directory.
When I use require_once('admin/database.php'); the database file has require_once(somefile.php) within it. There are numerous files like this being included.
How can I use include these files using require_once in the root directory without getting errors like these.
warrning: require_once(/Includes/simplecms-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\starter-ste\admin\Includes\connectDB.php on line 2
The includes folder is located within the /admin/ folder.
php require statements will look for files in the current directory and directories in the php_include_path. You can solve your problem by adding your include directories to include path.
You can do this dynamically by calling set_include_path
$directories = array('/library', 'var/', get_include_path());
set_include_path(implode(PATH_SEPARATOR, $directories));
[EDIT]
Your server might not know where the root directory of the website is. So, first, get the document's root directory and append it to the file name like this:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require_once "$root/your_file_path_here";
Looks like you haven't enclosed the file name within quotes. So that
line should probably be like this:
require_once("/Includes/simplecms-config.php");
I have a file which name is load.php near index.php in root folder. So i only have two files in root. I like clear works like most.
Today i needed to define directories in one file name is define.php. But it is not in root, i've added it to settings folder in root folder.
So my files:
index.php (requires load.php)
load.php (requires settings/define.php)
settings (includes define.php)
Now i don't know how to get root folders name?
__DIR__ in define.php gives me .../home/settings folder but i want to get root or another folders name. So what is the way?
To find out the "root" directory from inside settings/define.php, this should be sufficient:
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..');
See it in action.
Note: the linked example uses dirname(__FILE__) instead of __DIR__ because the latter is only available on PHP >= 5.3, but is functionally equivalent.