understanding php directories - php

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.

Related

How to define path for subfolder

I'm learning PHP and I'm having problems with the definition of the path.
If in the settings.php file located in the main folder I define a constant with the base path of the site to be able to use this constant in the subfolders I must include the file settngs.php precept by "../" as in the following way: ". ./settings.php "
is there any way to define the settings.php path that is also usable in the subfolder files without having to manually add "../" before the file name?
or a way to define constants usable in every file without including the file that contains the constants?
settings.php:
// FILE SETTINGS.PHP IN THE MAIN DIRECTORY
define('SITE_URL','http://localhost/');
admin/index.php:
// FILE INDEX.PHP IN THE ADMIN SUBDIRECTORY
include('../settings.php');
HOME
There is a way to have SITE_URL working without include('../settings.php') or there is a way to include "settings.php" in subdirectory without the "../" ??
Yes, you can use set_include_path function.
For example:
set_include_path(__DIR__.'/../');
Then include file from parent directory
include 'settings.php';

include file(s) from another directory

Currently I am trying to include a PHP file from another directory.
public_html/a/class/test.php <-- from this file i would to include a file from
public_html/b/common.php <-- wanted to include this file
Not sure what I should do because I have tried using
dirname(__FILE__)
and this keeps on returning public_html/a/ for me instead.
I have tried something like this
dirname(__FILE__).'/../b/common.php'
but it does not help me in getting my file.
You can simply keep moving up the directory tree until you have the common ancestor:
require dirname(dirname(__DIR__)) . '/b/common.php';
The magic constant __DIR__ equals dirname(__FILE__), and was introduced in 5.3. Each use of dirname() goes back one directory, i.e.:
dirname('public_html/a/class'); // public_html/a
dirname('public_html/a'); // public_html
Btw, editors such as PhpStorm also understand this use of relative paths.
First of all i suggest you to define a variable for basepath and include that defined variable in relative files.
// This should be on any root directory file
define("PR_BASEPATH", dirname(__FILE__));
And according to your implementation, Assume you are in
public_html/a/class/test.php
and dirname(__FILE__) returns the directory name of the current file that always return the directory class according to test.php file.
And you want to include public_html/b/common.php that is on the other directory /b. So you have to get the document root directory first.
include $_SERVER['DOCUMENT_ROOT'] . "/b/common.php";
Take a look on $_SERVER['DOCUMENT_ROOT']
include('../../b/common.php');
would include file for you, make sure both directory have same usergroup as user.

How to require_once in a function that is used in different directories

I need to require a file inside of a function like so:
function MyFunction(){
require_once('../../myfile.php');
//do stuff
}
This function may be used anywhere on the site so the path for the required file should not change. I tried using chdir(dirname(__FILE__)); but that changes the directory of my other files later on in the php, which is not desired.
When I am in various directories throughout the website I get an error message that the required file failed to open stream.
How can I do this?
I tend to use $_SERVER['DOCUMENT_ROOT'] for things like this. It relies on where your document root is set up in your virtual host file though.
You should use absolute paths in this case.
In your index.php you should get the location of your index.php file, that will be your reference point:
<?php
// store it in a constant, this info will not be changed anyway
define('APP_PATH',dirname(__FILE__));
//... rest of your code here
And, when you have to do a require_once for that file (let's say myfile.php is 2 directories above your index.php file):
require_once(dirname(dirname(APP_PATH)).DIRECTORY_SEPARATOR.'myfile.php');
or if myfile.php is in the same directory with index.php:
require_once(APP_PATH.DIRECTORY_SEPARATOR.'myfile.php');
Note
DIRECTORY_SEPARATOR is the PHP constant for the path delimiter (/ in any Linux, Unix, Mac, etc, or \ in Windows.
I am going to use require_once(__DIR__'/../myfile.php') this question provided a similar solution:
php require_once not working the way I want it to.. relative path issue

PHP: require doesn't work as it should

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/');

include file from different directory

following is my files directory structure...
Config
config.php (is calling some files too like includes/class.DB.php)
Now i have created another folder Admin and created new config.php (and calling root config file by require_once '../config/config.php';) its loading config file correctly but showing errors on includes/class.DB.php .
I hope, you have got my an idea of my problem, what is the way to achieve this, by USING DIR_NAME/ $SERVER['DOCUMENT_ROOT']
please help.
use
dirname(__FILE__); // or, if you have +5.3, use __DIR__ instead
so if you have this structure
includes
-class.php
admin
-admin.php
config
-config.php
So you can use
//admin.php
include(dirname(__FILE__)."/../config/config.php");
//config.php
include(dirname(__FILE__)."/../includes/class.php");
it always aims to the same directory!
Try:
include(dirname(__ FILE__)."/../include/connection.php");

Categories