include file from different directory - php

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");

Related

php how to include a file from a different root

For my website www.mysite.com I have a folder structure like this:
root
/login
/includes
In the folder login I have login.php with (simplified):
<?php
include_once('../includes/includes.php');
?>
If I access www.mysite.com/login/login.php the include works just fine, but if I access login.mysite.com/login.php the include doesn't work.
How can I get this to work?
I've tried using $_SERVER["document_root"] in the include, and I've tried with www.mysite.com/includes/includes.php.
Suggestions are much appreciated.
Edit: Unfortunately I don't have access to php.ini as I am on a web-hotel.
easy way with ssh you can Symlink file to anther locate file
ln -s my_folder/includes.php includes2.php
then use your include
include_once('includes2.php');
Thanks to #Professor Abronsius:
a combination of __DIR__ (magic constant), chdir(), getcwd() and set_include_path() are most useful when setting a path to files for inclusion (either using require or include etc ). You can store a reference to the current working directory with getcwd() for later use if you set different include paths or whatever.

PHP require folder above

I have a file structure that looks like this (unimportant files left out):
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
Now when I try to call require_once('../PHP/functions.php'); inside the "index.php" it doesn't work and I don't understand why. Anyone an idea what the problem could be?
This works fine:
require_once('../file.php');
first thanks for all the answers, I found the bug now:
inside the functions.php I require another file and the path to that file wasn't correct when I called it from index.php
So I had to change the path inside functions.php not inside index.php.
Still thanks to everyone for the other suggestions :)
Do you get an PHP error, and if what does it say?
Check for lowercase/uppercase problems
The following should work correctly:
require_once('../PHP/functions.php');
Check your privileges on the folder and try:
require_once('../PHP/functions.php')
You have a Folder Structure Like so:
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
And then, you are inside of index.php trying to Access the File functions.php, which lives inside of the Directory PHP. Then you need to go up one Directory from the current directory of the index.php File (ie. The Administration Directory) [meaning that you get up to Testserver Folder from theAdministration Folder] and then drill down to the PHP Directory. In code this would mean:
<?php
require_once __DIR__ . "/../PHP/functions.php";
Hope this does it for you.
Cheers & Good Luck....

How to properly include or require a file in an another folder from a file in an another folder

For example, I am going to require index.php (file found in config folder) in index.php which is in the anotherfolder. I am just doing this require dirname(__dir__).'config/index.php'; what I understand in this line is that it will get my working directory(blog1) and find the folder named config and see if there's an index.php in it. This would get the job done but I'm not sure if I am doing it right, and is my understanding is correct?
for example this is my directory:
blog1
config
index.php
anotherfolder
index.php
If anyone can help me with my little problem that would be great, (don't mind the - sign I don't know how to else illustrate a directory.
If you are in anotherfolder and want to include index.php from config then use this code:
require __DIR__ . '/config/index.php';
P.S It is a good practice to define a common set of constants like BASE_URL and BASE_PATH in a configuration file and then include files with absolute addressing by using BASE_PATH e.g.
require BASE_PATH . '/file_name.php';
You should also make a habbit of that.

understanding php directories

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.

PHP include in Joomla! 1.7

I need to include a file in one of my components in Joomla!. The file I want to include is located in the root of my Joomla! directory (where my index.php file is but my the file where the include is going is deeper in the administrator folder. It is in this file:
administrator/components/com_rsform/helpers/rsform.php
How can i put an include in that file that will link back to the file in my root of Joomla!? I need the path to be relative so that it is environment independent. Thanks in advance for your help!
Try the following:
<?php include_once (JPATH_ADMINISTRATOR .DS. 'components'.DS.'com_rsform'.DS.'helpers'.DS.'rsform.php'); ?>
There are also other variables that may help you:
JPATH_SITE = /your_local_path_to_site/ i.e: /var/www/mysite
JPATH_COMPONENT = /your_local_path_to_site/components/the_component_you_are_in i.e: /var/www/mysite/componentes/com_rsform
Hope that helps!
JPATH_SITE is constant always contains a path on the server's file system.
Please have a look onto this one.
http://www.latenight-coding.com/joomla-faqs/developers/get-path-joomla-folder.html

Categories