Referencing a file in a parent directory - php

Is this the right way to go about referencing a file in a parent directory relative to a php source file?:
require_once('../referenced_file.php');

To include a file relative to the current source file you should use the full path:
require_once dirname(__FILE__) . '/../referenced_file.php';
As the current working directory is set to the directory of the script that was initially executed.

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 PHP file from parent sibling folder in word press

I try to include some PHP file to another one by using include function.
It works fine when files are at the same directory that simply can do
include (file.php)
and if a file is in the child folder like
include (folder/file.php).
but I want to make my root WordPress folder project cleaner then change the location of my template files to pages-template folder but unfortunately, I can't include files from another folder that are the sibling with pages-template.
I try this
include '../inc/package-save.php';
but I got errors
Warning: include(../inc/package-save.php): failed to open stream: No such file or directory
any trick to fix this.
Finally, I found the solution.
just put the dirname(__FILE__) function before the directory that I want to include.
This function will get the directory path that files that I write this function to it.
for example, if file be at C:\xampp\htdocs\amt-master\wp-content\themes\wp-bootstrap-child it returns this path
But as my case, I want to get the previous path to do that I should just put this function in the same function like this
include dirname(dirname(__FILE__))."/filename.php"
then it will return this path
C:\xampp\htdocs\amt-master\wp-content\themes
then you can write remaining path.
Try to use the following
include TEMPLATEPATH . '/inc/package-save.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.

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.

Base Directory Change Issue

In my app, I'm using an AJAX call to retrieve information, which requires the inclusion of a db_connect.php file. db_connect.php requires other files for it to work.
If I include db_connect.php from the AJAX file, naturally, errors are returned from includes within the db_connect.php file because db_connect.php's includes are relative to the base directory of my application.
How can I change the working directory to the base directory of the application, so the included files will function properly?
What I've tried is using the chdir function:
echo getcwd() . "<br/>";
chdir("../../");
echo getcwd();
This correctly outputs:
/my/webserver/app/lib/ajax
/my/webserver/app
However, the include errors act like I haven't just changed the directory:
Warning: include_once(functions/clean_string.func.php) [function.include-once]: failed to open stream: No such file or directory in my/webserver/app/lib/ajax/ajax.php on line 8
What am I doing wrong?
Then use absolute paths.
For example in a config file, that resides in base of the app, define a base path that is absolute.
define("BASE_PATH", dirname(__FILE__)); //This now is a ABSOLUTE path to a dir that this file is in.
Then use:
include BASE_PATH . "/dir/dir/file.php";
Then, you just have to include the config file in every script at then top relatively from where you are in the app and then just use BASE_PATH in all other includes.
getcwd() does not return your include path; instead, try something like this:
chdir("../../");
include(getcwd()."/functions/clean_string.func.php");

Categories