I have made a config file that includes te database information (connection, and more).
The config file is called by index.php. So I have this:
index.php->calls->config.php->calls->db.php
This works, but sometimes the config.php is not called by the index.php but by for example header.php. The problem is that the relative path in the config.php to the db.php doesn't work.
How can I solve that? Should I use absolute paths?
Thanks!
If all the files are located in the same directory, you can include it like this:
include __DIR__."/config.php";
Otherwise create a constant in your index.php page before all includes that define your application path, and use that to include your files, E.G:
in index.php:
define('APP', __DIR__);
include APP.'/config/config.php';
in config.php:
include APP.'/lib/db.php';
Related
I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.
I'm having a problem with the folowing:
I have a file called config.php, which I include in two other pages: login.php and index.php. The pages are organized as follows:
index.php
Config/config.php
Page/Users/Login/login.php
lib/smarty.php
My problem, is that, within config.php, I have another include to a different file: smarty.php.
But because index.php and login.php are not on the same level, the smarty file only works for one of them:
Example : if I have, in config.php:
include('lib/smarty.php')
then it only works for index.php
if I have include('../../../lib/smarty.php')
then it only works for login.php
Is there a way for me to use the same config.php for both login.php and index.php and have smarty.php work for both?
I hope I was clear, and thank you in advance
NOTE: The problem is not how to include files from different folders into another, it is to include one file into two different pages in different directories and with different paths.
Use dirname(__FILE__), or if available, __DIR__ (PHP >= 5.3). They resolve to the full path of the directory the current file is in. It is better than explicitly using the current absolute path to the file because it allows you to move the files in the file system (as long as the files stay in the same location, relative to each other).
You'll have to change Config/config.php to include lib/smarty.php like so:
include(dirname(__FILE__) . '/../lib/smarty.php');
While you're at it, you should probably change your other include/require to use a similar construct.
Use the absolute path in all of your includes.
like:
include '/var/www/html/Config/config.php';
I'm working on some web application, learning PHP OOP. I have 3 scripts: index.php, database.php and config.php.
In config.php are the constants for accessing the DB.
In the database.php is a class for opening and closing the connection to the DB and so on. This file includes the config.php file.
Now on the index file I include the database.php file and I'm testing if $database exists. The files database.php and config.php are in different folder than the index.php.
When I run the index.php file it says: Use of undefined constant DB_SERVER - assumed 'DB_SERVER'
Everything works fine if they are in the same folder. It also works if I include the config.php file separately in the index file, but it won't work if only the database file is included
config.php
<?php constants ?>
database.php
<?php
require_once("config.php");
...
?>
index.php
<?php
require_once("../includes/database.php");
...
?>
Why won't the index.php file recognize the variables from config.php?
Its probably some problem with the include path. Even though database.php and config.php are in the same folder, its index.php that is making the calls and therefore the include path is the index.php folder. When database.php attempts to include config.php it will be looking for it there.
There are a couple ways you can solve this:
Make index.php call all the includes, including the database.php dependencies;
Make database.php aware that its path relative to the site root is ../includes and it too will refer to config.php as ../includes/config.php even though its right there in the same folder with it (not recommended specially if you need database.php in subfolders of your site);
Use set_include_path(). Example:
index.php:
set_include_path('../includes');
require_once 'database.php';
I believe the problem lies with the directories. When you include the ../includes/database.php file, all paths defined in that file become relative to IT! This means that instead of looking for the configuration file in the directory that index.php is in (lets say /var/www/html/funfile/index.php), it's now looking for it in /var/www/html/includes/ (so the literal path of the include would be /var/www/html/includes/config.php). It's a pretty easy fix, just make the path to the config file literal.
Try:
require_once("/var/www/html/funfile/config.php");
And replace the path I made up, with the actual path to the config file. That should solve your problem.
You should give DB_SERVER in quotes like this:
define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);
My database config class file is bellow
pard_config/class/Config.php
pard_config is a folder which is in the root folder.
And i have a json file which contain,host name,database,user and password details
pard_config/class/config.json
How would i include Config.json file to the Config.php file without reading it from the root.For now i'm doing it like bellow
Config.php
include("pard_config/class/config.json");
Config.json and the config.php files are in the same folder.So is there any way to read from that class folder like bellow ???
Config.php
include(config.json);
I would advice you to use a relative path and use the __DIR__ constant:
include(__DIR__ . '/config.json');
note that paths will be resolved from the script which where called in browser (or command line) .. like index.php. Therefore it will suite better in many cases to use a relative path.
I have a directory root:
index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
processInput.php:
require_once("testfile.php")
require_once("../testfile.php")
This code will work when you run index.php, of course it will not work when you run template.php.
As you can see, index.php includes template.php like normal. But in template.php, you have to include like if you are in the directory that index.php is in. But then, in processInput.php, you include as if you are in the directory that processInput.php is in.
Why is this happening, and how can I fix it so that the include path is always the directory of the file that the require is done in? The second included file have the same include path as the requested file, but the next one does not.
Thanks for your help!
EDIT: The strange thing is that I've included classes in a class folder. And it included other files as it is supposed to, even though the paths are relative. WHY does this happen, and how can I fix it?
VERY IMPORTANT EDIT: I just realized that all this is because in my example, the inclusion in includes/phpFiles/processInput.php includes a file in the same directory: require_once("file in same dir.php"); This is the reason. If you are including a file with out specifying anything more than the filename, the include_path is actually the dir where the file the require is written in is in. Can anyone confirm this?
Use an absolute path.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");
Use a similar form for all your required files and they will work no matter where you are.
You can do this in a few ways, amongst others:
Use set_include_path to control the directories from where to perform require() calls.
Define a common absolute base path in a constant that you define in index.php and use that in every require() statement (e.g. require(BASEPATH . '/includes/template.php')).
Use relative paths everywhere and leverage dirname(__FILE__) or __DIR__ to turn them into absolute paths. For instance: require(__DIR__ . '/phpFiles/processInput.php');
By default, the current working directory is used in the include path; you can verify this by inspecting the output of get_include_path(). However, this is not relative to where the include() is made from; it's relative to the main executing script.
You're using relative paths. You need to use absolute paths: $_SERVER['DOCUMENT_ROOT'].
When you include/require, you are basically temporarily moving all code from one file, to another.
so if file1.php (which is located in root) contains:
require("folder/file.php");
and you include file1.php in file2.php (which is in a different location (say folder directory for example):
file2.php:
require("../file1.php");
Now all of file1.php code is in file2.php. So file2.php will look like this:
require("../file1.php");
require("folder/file.php");//but because file2.php is already in the `folder` directory, this path does not exist...
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
Your directory structure is off. The file inclusion is being seen from the file you're using it from. So, "template.php" is looking for an "includes/" folder in its current folder (/includes/).
As others are saying, use absolute paths, which will make sure you're always going at it from the file system root, or use:
require_once("phpFiles/processInput.php")
In your template.php file (which is far more likely to break if you ever move things around, which is why others all recommend using absolute paths from the file system root).
BTW, if you're using "index.php" as some kind of framework system, you can consider defining a variable that stores the address of common files such as:
define('APPLICATION_PATH', realpath(dirname(__FILE__));
define('PHPFILES_PATH', APPLICAITON_PATH . '/includes/phpFiles/');