How to get parent directory relative to an absolute path - php

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

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

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.

PHP require_once error with relative path

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:
Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4
So it search the form.php file in include/../include/form.php what is wrong.
I used this code:
require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');
the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php
and the file what I want to test is in: C:\MyProject\include\form.php
What is the problem?
The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.
See the manual entry -> PHP Manual for __DIR__
__DIR__ will return C:\MyProject\phpunit-tesztek\include
You are looking to use the C:\MyProject\include\ path
__DIR__ . '/../../include/Form.php';
I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.
Try removing ../
require_once(DIR.'include/form.php');
Use magic constant DIR :
require_once(__DIR__ . '/../include/form.php');

Getting the path of a file in PHP three levels up from the current directory

I'm trying to access a folder three levels up from my working directory with file_put_contents(). Here is my working directory.
Users/myMac/Sites/mini-configurator/miniconfig/application/controllers
I need to get to the 'mini-configurator' folder and then into a subfolder of that called 'mc'. I can get the path of the file in PHP with dirname(__FILE__). How do I use that to get to the mini-configurator folder.
I tried dirname('../../../ . __FILE__), but it didn't work.
did you try
$path = dirname(__FILE__)."/../../../";
Having dealt with this recently, I've found something like:
$path = dirname(dirname(dirname(__DIR__)));
a little easier to read. YMMV.
Just use an absolute directory structure:
$path = $_SERVER["DOCUMENT_ROOT"] . "/../.../....";
//in the quotes just set it to the path from the root directory

How can i make a path file in php

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__)

Categories