include file(s) from another directory - php

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.

Related

PHP include file from other folder

Hey I am trying to include a file inside another file in PHP
If i write the entire path it does it with out problem.
$route = "/var/www/vhosts/aldroenergia.com/testmovil.aldroenergia.com/src/recursos/php/fEmail.php";
include($route);
but i would like to include with out writing the whole path.
Im including the fEmail.php inside
/var/www/vhosts/aldroenergia.com/testmovil.aldroenergia.com/src/ajax/correos/file.php
I've tried include("../../recursos/php/fEmail.php") but didnt work.
I've tried include(dirname(__FILE__."../../recursos/php/fEmail.php")); but failed too.
this is the folder structure.
--src
--recursos
--php
-fEmail.php
--ajax
--correos
-file.php
Constants paths and require statements are relative to the current file youre in.
To keep track of your paths I would suggest to use a central config file and define your root path in it:
# ./src/config.php
define("ROOT", __DIR__);
# Later include config.php and use:
require_once(ROOT . "/src/recursos/php/fEmail.php");
include(dirname(__FILE__."../../recursos/php/fEmail.php"));
when you are passing the result of __FILE__ concatenate to the ../../recursos/php/fEmail.php within the dirname function it will not work because It's a wrong path.
you must instead past just __FILE__ as parameter to dirname which will return the absolute path of the directory in which the file.php file is. after getting the contening directory path you can past the relative path to the fEmail.php file from file.php directory.
include(dirname(__FILE__) . "/../../recursos/php/fEmail.php");

php question include an included file

I got a problem about including a included file in PHP.
Project
functions(folder) has a.php
xml(folder) has b.xml
index.php
This is my project structure(sorry about that, I can't post images).
I try to use "index.php" to include "a.php" while "a.php" is using "b.xml"
this is what i did on XAMPP and it works perfectly:
in index.php I wrote: include 'functions/a.php';
in a.php I wrote: $xml->load('xml/b.xml');
However if I copy these to my Uni's apache server, it can't open this b.xml.
This is not permission because when i change to absolute path it works...
Thank you guys in advance:-)
in a.php you should refer to ../xml/b.xml if you use include
thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
Try using set_include_path() to set the include path to your application's root directory; then you should be able to include files relative to this path.
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml'; )
If you can't do that, you should add xml's parent to your path.

Relative include files

I have a file
workers/activity/bulk_action.php which includes a file
include('../../classes/aclass.php');
Inside aclass.php it does:
include ('../tcpdf/config/lang/eng.php');
It seems that the include in the second file is using the first files working directory instead of being relative to itself, resulting in an error. How does this work?
You can adapt the second include with:
include (__DIR__.'/../tcpdf/config/lang/eng.php');
The magic constant __DIR__ refers to the current .php file, and by appending the relative path after that, will lead to the correct location.
But it only works since PHP5.3 and you would have to use the dirname(__FILE__) construct instead if you need compatibility to older setups.
You would be way better off by setting a proper value to the include_path and then use paths relative to this directory.
set_include_path(
get_include_path() .
PATH_SEPARATOR .
realpath(__DIR__ . '/your/lib')
);
include 'tcpdf/config/lang/eng.php';
include 'classes/aclass.php';
I also suggest you take a look at autoloading. This will make file includes obsolete.
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing.
You can use dirname(__FILE__) to get a path to the directory where the currently executed script resides:
include(dirname(dirname(__FILE__)) . '/tcpdf/config/lang/eng.php');
(since PHP 5.3, you can use __DIR__)
Or, define a constant in the first file that points to the root directory and use it in your includes.

PHP including files

What's the difference between
$_SERVER['DOCUMENT_ROOT'];
and
dirname(__FILE__);
I wonder what's the difference because when I 'echo' them, they're returning same path. Which do you prefer should I use and why?
Thanks!
Both are different
_FILE_
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
source : PHP magic constants
Let's said, your document is /var/www,
and your index page is /var/www/index.php
dirname(__FILE__) == $_SERVER['DOCUMENT_ROOT'];
But if you drill-down to sub-folder like /var/www/posts/index.php
dirname(__FILE__) != $_SERVER['DOCUMENT_ROOT'];
/var/www/posts != /var/www
The use of $_SERVER['DOCUMENT_ROOT'] is more appropriate in this case.
__FILE__ always points to the current file path, and $_SERVER['DOCUMENT_ROOT'] points to the document root path ;-)
I prefer first one, as it is more semantic.
If you will try to compare the values of the files, that are located not in your docroot - then you'll get different values.
The former one is a root folder for the HTTP server (or VirtualHost) and it is a server setting.
The latter is the folder containing the current file.
The usage is entirely based on requirements in my opinion.
You would normally use $_SERVER['DOCUMENT_ROOT'] when you want to reference your website's root folder from any where within your website or web application.
You will find using dirname(__FILE__) handy if you were including a file, that then needed to include some more files from the same directory. I use this in my PHP wrapper for the Dribbble API
class Dribbble {
function __construct() {
require_once(dirname(__FILE__) . '/base.php');
require_once(dirname(__FILE__) . '/shot.php');
require_once(dirname(__FILE__) . '/player.php');
}
}
This means I can just include dribbble.php from any where in my website or web application and not worry about also including base.php, shot.php, and player.php at the same time.

How to include file from another directory

In the root (www) I have two folders.
In the first folder, "folder1", I put a file called register.php.
In the next folder, "folder2", I put files called header.php and footer.php.
I need to include the header and footer files from folder2 in the register.php file.
How can i do this? I tried to use this include ../folder2/header.php
..but it does not work
On some configurations, adding ./ (current dir) does the trick like this:
include './../folder2/header.php';
Alternatively, you can specify in terms of document root:
include $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php';
<?php include( $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php' ); ?>
include $_SERVER['DOCUMENT_ROOT'] . '/folder2/header.php';
would work from any directory of the site
it is called absolute path and it's the only reliable way to address a file
However, in real it should be something like
include $_SERVER['DOCUMENT_ROOT'] . '/cfg.php';
// some code
include $TPL_HEADER;
using a variable, previously defined in cfg.php
However, it may fail too. Because you can be just wrong about these paths
And here goes your main problem:
but it does not work
There is no such thing as "it does not work"
There is always a comprehensive error message that tells you what exactly doesn't work and what it does instead. You didn't read it yourself, and you didn't post it here to let us show you a correct path out of these error messages.
include files should generally be kept outside of the server root.
lets say your setup is;
www/website1
and
www/includes
Then you php.ini file, or .htaccess file should stipulate that
include_path=www/includes
then from any of your files, in any directory, no matter how far down the trees they go you simply do:
include 'myfile.php';
where myfile.php is at www/includes/myfile.php
Then you can stop worrying about these issues
include dirname(__FILE__).'/../folder2/header.php';
Try This it is work in my case
<?php require_once __DIR__."/../filename.php";?>
As the PHP manual states here $_SERVER['DOCUMENT_ROOT'] is "The document root directory under which the current script is executing, as defined in the server's configuration file." For this example, $_SERVER['DOCUMENT_ROOT'] will work just fine but. . . By using the new "magic constants" provided in >= PHP 5.3, we can make this code a little safer.
Put your includes in a subfolder, and use the magic constant DIR to make a reference to the included files. DIR returns the directory of the currently executing php file. By using this, you can move your folder containing all your includes anywhere you like in your directory structure, and not need to worry if your includes will still work.

Categories