I was wondering how can I make an path file in php?
For example I would like to have a pointer file in the root folder that points to the folder where the php script are held.
I tried something like this, but it does not work.
path.php (is in the root file eg. htdocs/project1/week5)
$path = "/project1/week5/php";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
alert.php (is in the root file eg. htdocs/project1/week5)
include("path.php");
include("AlertFormAction.php");
AlertFormAction.php (is in htdocs/project1/week5/php)
What am I doing wrong? Could somebody be so kind and show me how it is supposed to be done? Thank you.
Your $path variable contains an absolute path that in most likelihood, does not exist.
Paths in PHP are local (to the server) filesystem paths. At a guess, I'd say you want to try
$path = '/htdocs/project1/week5/php';
As the include path "php" seems to be relative to your path.php file, you may find this more flexible
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__ . '/php',
get_include_path()
)));
If using PHP 5.2 or lower, replace __DIR__ with dirname(__FILE__)
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');
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");
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 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.
I have a PHP file at my server root.. index.php .. which include's .. DIR/main.php
Now .. DIR/main.php .. include's many nearby PHP files using relative URLs using .. include("./common1.php");
Any way I can change the relative-URL base path so when including "DIR/main.php" It can relatively access its nearby PHP files like "DIR/common1.php", instead of trying to find "common1.php" at the site root.
Take a look at set_include_path
Edit: When appending paths to include_path be sure to use the PATH_SEPARATOR constant as it is intended to make your include path OS agnostic.
<?php
set_include_path(implode(PATH_SEPARATOR, array(
get_include_path(),
'/DIR1',
'/DIR2/DIR3',
dirname(__FILE__),
)));
?>
First, set the "relative-URL base path" to your directory
set_include_path( get_include_path() . '/DIR' );
Second, include your file!
require( 'main.php' );
That should work though I've not tested it.
In addition to lonut's answer, you can get the directory of the file using a combination of the __FILE__ constant and dirname().
$include_path = dirname(__FILE__);
set_include_path($include_path);