PHP: include file relative to calling file NOT root file? - php

OK say I have a file called fileA.php in the same directory I have another file called fileB.php
so inside fileA there is this require_once('fileB.php') notice I did not specify a file path because both files are in the same directory.
Now if fileA is included into another file that is located somewhere else, it fails to include fileB because the parent file (the file that included fileA) is not in the same directory as fileB.
So how can I make it so that fileA loads fileB no matter where fileA is included into?
Does that make sense?
Thanks!!

Assuming both fileA.php and fileB.php are guaranteed to be in the same directory, this should work:
// fileA.php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR .'fileB.php')

http://php.net/manual/en/function.include.php#78716
You can set the include root to your website root.

Related

Can't get result with file_exists()

I've been trying to delete a .json file with php on my site, and can't get it to work. It seems I can't get the path to the file right, or it's invisible. So I thought I'd check with file_exists(), and no matter what I try, I cannot get the system to see a file that is on my server.
the json folder exists in this path
/mytheme/assets/js/geofences/1069.json
The php is being run from this path
/mytheme/inc/filelookingforjson.php
I have tried "../assets/js.." to send path pointer up one level using relative path...
I have tried absolute filepaths. and it's behaving like the file isn't there. Is there some htaccess or some other thing i need to do on a wordpress site? Permissions on the geofences folder are set to read, writeable etc... the code below sends me an email saying there is no file. I'm sure I have the path wrong, if i can get this correct than i can work on the unlink... thank you for any input.
$file = '/wp-content/themes/mytheme/assets/js/geofences/1069.json';
if(file_exists($file)){
mail("me#email.com","file exists","file name is there");
} else {me#email.com","file does not exist found","file name is
not there");
}
The issue
If the folder structure is:
mytheme/
inc/
filelookingforjson.php
assets/
js/
geofences/
1069.json
then the path to the json file from the PHP file would be:
../assets/js/geofences/1069.json
However, considering that your PHP file most likely gets included by other PHP files, the path will be relative from the first accessed PHP file.
Why is this?
Imagine this file structure:
index.php
folder/
foo.php
subfolder/
bar.php
assets/
hello.txt
Let's say that you want to read the file hello.txt in the file bar.php, then the relative path to hello.txt would be ../../assets/hello.txt. This would work if you run the file bar.php directly.
The gotcha
The "gotcha" is when you're not accessing bar.php directly.
Let's now say that index.php includes foo.php and foo.php includes bar.php.
If you run index.php, the relative path in bar.php won't work anymore since it isn't relative from the first accessed PHP file in the include chain, which is index.php.
This does make sense if you think of include and require as a "copy/paste". PHP "copys" the code from the included file and "pastes" it into the file that does the including.
Note: this is of course a simplification of how it actually works
Solution
The magic constant __DIR__.
If you add that before your relative path, PHP will replace it with the absolute path to the file it was written in.
The code:
$file = __DIR__ . '/../../assets/hello.txt';
will be read by PHP as:
$file = '/folder/subfolder/../../assets/hello.txt';
Now it won't matter what file includes bar.phpsince the path will stay absolute.
The same thing goes when you're trying to include files using relative paths.
On a real server, the absolute path would probably look something like /path/from/the/file-systems/root/folder/assets/hello.txt.

PHP include file that includes a file up the directory tree

I have this directory tree:
test.php
foodir
`---- foo.php
bardir
`---- bar.php
When I open test.php, I include foo.php. Then, I want foo.php to include bar.php.
test.php:
include 'foodir/foo.php';
foo.php:
include '../bardir/bar.php';
However, when I open test.php, I get:
Warning: include(../bardir/bar.php): failed to open stream: No such file or directory
I noticed that if I change my directory tree to:
test.php
foodir
`---- foo.php
---- bardir
`---- bar.php
And then change foo.php to:
include 'bardir/bar.php';
Everything works. It appears that I can include files relative to the currently included file.
However, why am I not able to travel up the directory tree of that file?
Edit:
I know that I can put include 'bardir/bar.php' in foo.php. It would search for bar.php in the location of test.php. However, that doesn't solve my problem if I include foo.php from a file in a directory other than the one where test.php is. That's because in that other directory, bardir/bar.php wouldn't exist.
When you use include 'foodir/foo.php'; that file is now basically running in the "scope" of test.php.
Therefore, the include inside that file include '../bardir/bar.php';
will go up one folder and THEN searching for bardir, which isnt there:
(it's looking for this arrangement:)
parent/test.php
parent/foodir
`---- foo.php
bardir
`---- bar.php
so, the correct include to use inside foo.php would be include 'bardir/bar.php';
in short: If you open test.php all include-paths used in any included file should be relative to the location of test.php - not to their actual location.
ps.: As mentioned in the comments: Includes are first checking the defined include-dir, only if there isn't a match, the path is considered "relative".
Simply do:
include dirname(__DIR__).'/bardir/bar.php';
__DIR__ is the absolute directory path of the file where this constant is used/called (no matter where it's included from), thus __DIR__ is equal to /full/absolute/path/foodir. and dirname(__DIR__) goes one directory up of the path so dirname(__DIR__) /full/absolute/path, and where the bar.php resides is /full/absolute/path/bardir. That's what we want, that's what we get.
This way you don't have to worry include path relativity. It will work any case, no matter where foodir/foo.php is included from
"When you use include it assume the base directory to look for
file is the directory of current file"
So when you have to include file from the another dir you have to specify the relative path from parent directory as here "Base" to include so let say for
Base -> A
->file1.php
->B
->file1.php
we will use
include "../A/file1.php";
for Adding files from A and vice-versa.
Assume ../ as the one level up of specified dir
also you can use __DIR__ like build in constants for getting the dir name of current file

path issue with include in PHP

I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.

php included files and directories

Lets say I have two files in directory new_folder: a.php and b.php
In the file of a.php there is a statement:
include_once 'b.php'
In addition, I have a file demo.php in a parent folder, in the demo file there is a statement:
include_once 'new_folder/a.php'
I.E. the folder structure is:
demo.php
new_folder
- a.php
- b.php
Why if I write in the a.php file:
include_once 'b.php' - correct path
include_once './b.php' - incorrect path
include_once 'new_folder/b.php' - incorrect path
Try This
require_once('../b.php');
my answer is based on php.net:
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.
that means that if I write in a.php: include_once 'b.php' ; and then include a.php in demo.php then first it looks for the file in the include_path, then it will look for the file in the directory of the calling script (a.php), and only then it will look for the file in the working directory (that is the directory of the file that we run and starts all the includes- demo.php)
edit: I tried running it and found out that first if there is b.php in the working directory then it will include it and not the b.php from the calling scrip's own directory.
the last check for the file is in the new_folder (the calling script's own directory)

Referencing a file in a parent directory

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.

Categories