Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file from a different folder in PHP

Let’s say I have two folders in my PHP server root directory: x and y. There are two more directories inside the x folder: a and b. In the /x/a directory I have the index.php file and in the /y folder a stuff.php file.
From my /x/a/index.php file, I want to include the /y/stuff.php file. How do I do that?
My server doesn’t allow including files from other domains, so adding the full URL doesn’t work! Also, I’d like to know how to start a path from the root in PHP. I use ./blabla from the index of my root and ../blabla from directories in the root, but, unfortunately, .../blabla doesn’t work from 2nd grade directories.

like image 886
Vercas Avatar asked Jan 26 '26 20:01

Vercas


2 Answers

To access the root begin your path with "/".

If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../".

If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).

like image 117
Adam Heath Avatar answered Jan 29 '26 08:01

Adam Heath


try:

include "../../y/stuff.php";
like image 34
Chuck Morris Avatar answered Jan 29 '26 09:01

Chuck Morris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!