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");
Related
I am trying to require a file in PHP that is itself a required file. The path to the directory should be ../../includes/database.php. But instead it is trying to go up two directories from the initial file requiring this second one.
I have found out that __DIR__ can be used to get the absolute directory of the file. However I still cannot link to the correct directory using something like:
require(__DIR__ . '/../../includes/database.php');
Is there some way to get a directory relative from this DIR path?
You can try like this:
chdir('../');
echo getcwd();
You can use:
require(dirname(__FILE__) . '/../../database.php');
I've searched ways to do it so it would work but none of them seemed to work for all paths, so I was wondering if you could give me a direction on how to do this:
This is the structure:
Home Directoy
config.php
index.php
includes/
user_login.php
applicant_track.php
ucp/
index.php
When I am trying to include includes/user_login.php in ucp/index.php it doesn't let me and says that it cannot find the file, my code is in ucp/index.php is:
if(!defined('root_path'))
{
define('root_path', realpath(dirname(__FILE__)));
}
include(root_path . '\config.php');
switch($_GET["a"])
{
case null: include(root_path . '\index.php'); break;
case "login": include(root_path . '\includes\user_login.php'); break;
}
This is what I get:
Warning: include(E:\xampp\htdocs\aod_panel\ucp\config.php): failed to open stream:
I'd be happy for an advise on how to fix this.
Use following path
define('root_path', realpath(dirname(dirname(__FILE__))));
instead of your code for defining real path. As your folder structure is like that.
See your index.php is in ucp folder but you want path of config.php. So go back one directory and get config.php path.
Your root path does not points to the actual root of your project. Your actual root is someLocation/yourProject.
the root you have defined is someLocation/yourProject/includes/
Then you want to include file in another folder. Hence it cannot find it. Define the root of your path to your actual project root and not inside includes directory.
To do this, you can define the root path in your config file and read it from there.
This is most likely one of your other scripts includes the config.php without proper path. Set up your include paths using the following code
set_include_path(get_include_path() . PATH_SEPARATOR . $root_path);
Also change the include to require_once to prevent multiple includes.
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.
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.
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.