I have a website with a subdomain (my.domain.com)
my main website uses
include 'directory/page.php';
and on the subdomain i want to be able to include the same file, i have tried:
include realpath(dirname($_SERVER["SCRIPT_FILENAME"]) . '/../..').'/directory/page.php';
but i get an error saying the file cannot be found.
My directory structure looks like:
www.domain.com - /home/username/public_html
my.domain.com - /home/username/public_html/my
how can i include a file in the public_html directory in the my. subdomain?
include dirname(__DIR__).'/directory/page.php
Refer this to learn more
Simply,
include_once $_SERVER['DOCUMENT_ROOT'] . '/../directory/page.php';
Related
I've a web site made of some folders, one for each section (info, news, blog etc...).
In each of these folders there is an index.php file that should load a layout (common to all). These are stored in a different folder in the root where there is also the main index.php file (the homepage). So i have something like this:
root
-index.php (home)
-/layout
--layout files
-/info
--index.php`
The index file in the /info folder should include the page layout from /layout.
The problem is that the layout files should include other files from other folders.
In layout files I put this:
include 'contents/page-element.php';
But if I try to reach the same page-element.php file from the index.php file in the /info folder, I should do:
include '../contents/page-element.php';
to go to the root and then reach the /layout folder.
I don't want to create a copy of the layout for the folders so I've tried $_SERVER['DOCUMENT_ROOT'], but it does not work in some cases on the localhost and even on the web server.
Can someone help me or let me know how I can build a dynamic absolute path?
By using the built-in constant __DIR__ you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php
I'm unable to include files in a folder from the root. Here's something similar to what I'm trying to do.
Root:
-folder1
--folder2 (I need to include stuff in this folder)
-otherfolder1
--otherfolder2
--otherfolder3 (This is my website folder)
This is what I have for my path so far which doesn't work.
define("FOLDER2_ROOT", "/folder1/folder2/");
../ is used to go up one folder.
So according to your situation, you need the following code:
include("../../../folder1/folder2/abc.php");
So my folder is called
leaguenotes and inside that folder i have a folder administration which is my subdomain folder so
leaguenotes
-index.php
-js
-files.js
-administration
-index.php
-otherfiles.php
And from administration index I'm trying to include in this example files.js with <?php echo '<script src="'.dirname($_SERVER['DOCUMENT_ROOT']).'/js/jquery.min.js"></script>'; ?>
$_SERVER['DOCUMENT_ROOT'] = /var/www/html/leaguenotes/administration which is the subdomain folder so I'm using dirname to get to parent folder /var/www/html/leaguenotes and add /js/jquery.min.js and still the file is not found
if you access your page, take a look at the html source-code, i think you will see the source produced from your php code is not the correct one. because a real subdomain is a different domain then your domain itself. It is treated by your browser as a different domain.
i have a site which has a subdomain, for example domian.com and forum.domain.com.
in my domain.com i have inc folder which contain my initialize.php and config.php my classes and functions in it. i just include initialize.php in each php page and other pages included in initialize.php like config.php
in config.php i define
define("BASE_URL","/");
define("ROOT_PATH",$_SERVER['DOCUMENT_ROOT']."/")
now when I include initialize in subdomain it dosen't work because ROOT_PATH assumed as subdomain. in this situation what sholud I do? how can I use files that are in domian.com root in subdomain?
should I copy all off them in subdomain?
It can be done with a simple trick like following,
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../inc/header.php';
I recommend you to read the following tutorial
https://developer.hyvor.com/php/including-files-in-root-with-subdomain.php
I've searched ways to do it so it would work but none of them seemed to work for all paths, so I was wondering if you could give me a direction on how to do this:
This is the structure:
Home Directoy
config.php
index.php
includes/
user_login.php
applicant_track.php
ucp/
index.php
When I am trying to include includes/user_login.php in ucp/index.php it doesn't let me and says that it cannot find the file, my code is in ucp/index.php is:
if(!defined('root_path'))
{
define('root_path', realpath(dirname(__FILE__)));
}
include(root_path . '\config.php');
switch($_GET["a"])
{
case null: include(root_path . '\index.php'); break;
case "login": include(root_path . '\includes\user_login.php'); break;
}
This is what I get:
Warning: include(E:\xampp\htdocs\aod_panel\ucp\config.php): failed to open stream:
I'd be happy for an advise on how to fix this.
Use following path
define('root_path', realpath(dirname(dirname(__FILE__))));
instead of your code for defining real path. As your folder structure is like that.
See your index.php is in ucp folder but you want path of config.php. So go back one directory and get config.php path.
Your root path does not points to the actual root of your project. Your actual root is someLocation/yourProject.
the root you have defined is someLocation/yourProject/includes/
Then you want to include file in another folder. Hence it cannot find it. Define the root of your path to your actual project root and not inside includes directory.
To do this, you can define the root path in your config file and read it from there.
This is most likely one of your other scripts includes the config.php without proper path. Set up your include paths using the following code
set_include_path(get_include_path() . PATH_SEPARATOR . $root_path);
Also change the include to require_once to prevent multiple includes.