Open file from a different folder in PHP - 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.

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

try:
include "../../y/stuff.php";

Related

How can i Include a php file in a directory which contain include('xyz.php'); from another directory?

My directory listing is: Users>Images>PreDefines
Where i am creating a php file i.e. index.php in PreDefines Folder. I want to include a php file from Users Folder i.e include('header.php');.
But There is a problem. header.php file contain include('title.php'); which not coming in PreDefines>index.php what should i do to include a file from another directory which includes others php files also.
you can simply navigate with ../ which means go backward one step and then go wherever you want
Assuming you are in Users/Images/Predefines/index.php and you want to include a file in Users you say include('../../header.php');
Or for example you have another folder in users called assets which has a file index.php.
You do: include('../../assets/index.php);
Always include in absolute to the directory you are working on.
Something that might help php has getcwd() which returns the current working directory.
Also if you are going to change the server such as from windows to linux or other type of servers instead of / use the DIRECTORY_SEPARATOR constant which will place a separator according to your server for example
include('..'.DIRECTORY_SEPARATOR.'title.php')
Hope i understood your question

PHP auto detect upload url path

I work in responsivefilemanager and in config file I Have two line:
$upload_dir = '/user/uploads/files/'; // path from base_url to base of upload folder (with start and final /)
$current_path = '../../../../uploads/files/'; // relative path from filemanager folder to upload folder (with final /)
This wroked In xammp system (with sub folder. /user/ is sub folder). if i move in real server i need to edit this two line and remove sub folder and one ../ from two line.
Now, I need to auto detect url path. My mean is : if i install in sub folder Or root folder this two line worked in my script without manual editing.
How do can i create this?
There is no particular way to just autodetect what directory to upload to. The easiest is to solve this by just checking if the directory exists and use the other directory if it was not found.
However, I'd suggest having the same relative structure on both systems is the best solution. With the same relative structure this should work across all servers and systems without having to change anything.

access root file from subdomain in PHP

Is it possible to include file from the ftp's root in a script in a subdomain ?
This doesn't work for me:
include($_SERVER['DOCUMENT_ROOT'].'/joomla.php');
Can someone help me ?
I think first you need to determine where is your subdomain files are placed and echo the $_SERVER['DOCUMENT_ROOT'] command to see the full path then you can combine two paths and make necessary adjustments.
Usually but not always, subdomain files are placed in a folder one level up then the main site's root folder so you may need to replace the folder name according to your needs in the output of server document folder function.
To give an example if your domain files are in a path like
/var/www/vhosts/example.com/httpdocs/
and if your subdomain path is like
/var/www/vhosts/example.com/subdomain/
then you will need to replace httpdocs with subdomain (or the other way around) in the output where you call $_SERVER['DOCUMENT_ROOT']

PHP MKDIR inside server root but outside domain root folder?

I have a domain root which is a folder within the server root. How can I mkdir inside a different folder within the domain root?
ROOT > DOMAIN ROOT FOLDER > PHP MKDIR
ROOT > IMAGE FOLDER > MKDIR HERE
I need the php in the 'domain root folder' to create a directory within the 'image folder'. Any help?
You can usually do something like this:
mkdir('../images/newfolder');
You may have to add more ../../ on to the front of this, depending on where you are running the script from exactly. Each ../ moves you up another level, so it seems like just one would work here given your example, but you have to be sure you are where you think you are.
You could also use absolute path in your php-script.
mkdir('/root/images/new_folder');
Make sure that your web-server has permissions for writing to specific folder.
I usally work from the directory my current script is located in and maneuver my way to the desired location from there. Works fine as long as you don't move you script. In addition, it will be easier to keep track of where everything is relative to the script.
You can get the current path with:
$path = dirname(__FILE__);
Then appending ../ to go deeper etc.

how does the php copy function destination string ( ./ )work?

Okay so I'm learning about uploads and need to copy the tmp file to a specified directory so I use the copy command.
I use this inside my index.php of the main directory (learningupload/ folder)
copy($_FILES['photo']['tmp_name']['file'], './pics/'.$photoname.'.jpg');
Now I'm doing this on Xampp local host, and my uploads go to the tmp folder which is of course different from where I want the upload to go
so there's
C:\xampp\tmp
C:\xampp\htdocs\learningupload\pics\
My question is this: What is this ./ inside the destination string I need to have? Doesn't it usually mean you're moving UP a directory? Why not have it '/pics' no dot? I tried that and said it couldn't open the stream. Does the dot here mean it's referring to whatever directory index.php is in? Or am I thinking ../ moves up a directory? And single dot is different?
Thanks.
./ means "current working directory". Which directory is current working one you can see with echo getcwd();
The parent directory (the upper one) is ../
You can change your working directory using chdir() or you can just specify the full path instead.

Categories