access root file from subdomain in PHP - 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']

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

put config in secure place, can not link the files

Am trying to use a config file for a database and rating script but the problem is the config file is in this directory :
website.com/include/config.php aka websitename/include/config.php
The rating script needs the config and is accessed like this:
include_once("config.php");
I want the config to be in:
"/files/website/"
A directory level up from the website root folder.
I have been trying with:
"../files/website/" and other variations but can not figure out how to link them.
I have managed to put one config file and access it, but with this ajax rating script the only way for it to work is to have the config in the /include/ folder next to:
rating_process.php - has this link : include("inc/config.php");
rating_functions.php - has this link : include_once("config.php");
rating_total_functions.php - has this link : include("inc/config.php");
Hope i've explained myself here
Right, looking at my hosting now:
$_SERVER['DOCUMENT_ROOT']; outputs this: /foldy/homepages/11/username/htdocs/webbysite
My index file is located at: /foldy/homepages/11/username/htdocs/webbysite/index.php
The included rating script is located in: /foldy/homepages/11/username/htdocs/webbysite/include/
I want the config to be in /foldy/homepages/11/username/htdocs/secretfiles/config.php
Am trying to some how go out of: webbysite folder and then into secretfiles (sibling folders)
I have tried adding ../ and so on, but am missing something obviously :(
Try
$configLocation = $_SERVER['DOCUMENT_ROOT'].'../files/website/config.php';
include_once($configLocation)
but the problem is the config file is in this directory
it is not a problem at all.
just keep it as is.
Concerning your particular problem, your problem is that you don't know where you want to put your file. /files/website/ is not likely a right path and it is apparently not one level high from webroot.
So, first of all make your mind about the right path to the directory and it's relative position to the web root
if you are concerned about security ( because your config file contains the db details ) i would place the db config file outside the site root folder and then require_once('../../dbConfig.php') from the script that's creating xml or json for your ajax
more exactly ...
your site folder might be here: /var/www/html
set a virtual host (done differently on Linux and Windows) and point your domain to a sub folder inside /html so that the new path to the site root is /var/www/html/site.
then place your config file in /var/www/html and call it from your scripts inside your /site folder using require_once('../dbConfig.php)`.
your db details are outside the site folder

how can I get a URL from an absolute folder path?

I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com is unnecessary and useless. just /myfolder/ is what you really need.
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)

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

Categories