So I have some php files that I'm trying to include with no luck.
I have the root directory, and off that I have a directory called aboutus. In the aboutus directory, I'm trying to include a css file from the root. So I use this code:
include $_SERVER['DOCUMENT_ROOT'] . "/meta-head.php";
The include works fine, but in the meta-head.php there are some calls to files, one of which is:
<script src="_assets/js/navbar.js"></script>
The problem is that it's trying to access this from root/aboutus/_assets/js/navbar.js (which doesn't exist), when I want it to acccess it at root/_assets/js/navbar.js.
What am I doing wrong so that it won't access the file relative to the root?
Thank you!
use absolute path or use this:
<script src="/_assets/js/navbar.js"></script>
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
When attempting to link from the root directory in my PHP file, I am unable to load the file. It works fine unless I add the file path as absolute, like so:
<link href="/supportfiles/subheader/css/css.css" rel="stylesheet" type="text/css">
<div id="subheadingContainer">
<div id="allContentContainer">
<?php
if (count($loggedIn["directReports"]) != 0 ) {
require("/supportfiles/subheader/itemshtml/inbox.php");
} else {
require("/supportfiles/subheader/itemshtml/myrequests.php");
}
?>
</div>
</div>
Any idea why it works fine if I ../ but not if I hard code it? This is a support heading file, so im using it across multiple levels in directories and I think it should be hard coded from the root.
When you use absolute paths in PHP, / refers to the root of your file system, not the root of your web application, this means that if you want to use absolute path you have to use the path as if you accessed from your file manager, not from the browser.
For example lets say that you are working on linux with apache and that the root directory of your apache server is /var/www/ then you would have to put:
require("/var/www/supportfiles/subheader/itemshtml/inbox.php");
if you are unsure of your document root you can use the PHP's $_SERVER['DOCUMENT_ROOT']. so in your case just put $_SERVER['DOCUMENT_ROOT'] before any absolute path and this must work just fine. so for example:
require $_SERVER['DOCUMENT_ROOT'] . '/supportfiles/subheader/itemshtml/inbox.php';
My question is similar to PHP include file strategy needed. I have the following folder structure:
/root/pages.php
/root/articles/pages.php
/root/includes/include_files.php
/root/img/images.jpg
All pages in the "/root" and "/root/articles" directories have a "header.php" and "footer.php" file included within them, which are stored within the "/root/includes" directory.
The header and footer pages both contain images stored in the "root/img/" directory.
As suggested by:
how it`s possible to include a file (include();) from the root folder to a files from different subfolders?
How to use a PHP includes across multiple directories/sub directories with relative paths
I tried the **dirname** solution and the $_SERVER['DOCUMENT_ROOT] solution to include the header and footer in the files stored in the root directory and articles subdirectory.
However, I run into issues when I try and use either method (within the header and footer files) to link images:
<img src=" <?php echo dirname(__FILE__). '/img/image.jpg';?>">
Chrome fails to display the images and throws the following error:
locally: Not allowed to load local resource: file:///C:/root/img/image.jpg
on the server: GET http://host.com/home/public_html/root/img/image.jpg 500 (Internal Server Error)
I have checked the URL's and they appear to be correct. Having googled for solutions, I found that chrome prevents the display of files that contain it's complete file path for security reasons.
How do I get around this? or is there an alternative way to include files (including images) into files stored in any subdirectory?
#sandeep's answer is partially right. because
$_SERVER['DOCUMENT_ROOT'];
will again give back fully qualified path to the root.
<img src=" <?php echo 'http://localhost/favicon.ico';?>"/>
will return image back because now I am not giving it local path as per your problem but url of the server I am running.
for your included scripts try setting the includes_path using set_include_path
set_include_path( $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR );
Then, to include a script within your pages you can use:
include( 'script.php' );
For the images, prefix the path with a leading slash / ~ ie:
<img src='/img/image1.jpg' />
It tends to be easier to use root relative paths rather than directory relative paths so prefixing with the leading slash means a folder within the root.
This may be help you
$url = $_SERVER['DOCUMENT_ROOT'].'/img/image.jpg';
<img src="<?=$url?>">
Use ../img/image.jpg for including in files inside /root/articles.
Use img/image.jpg for including in files inside /root
I have a directory root:
index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
processInput.php:
require_once("testfile.php")
require_once("../testfile.php")
This code will work when you run index.php, of course it will not work when you run template.php.
As you can see, index.php includes template.php like normal. But in template.php, you have to include like if you are in the directory that index.php is in. But then, in processInput.php, you include as if you are in the directory that processInput.php is in.
Why is this happening, and how can I fix it so that the include path is always the directory of the file that the require is done in? The second included file have the same include path as the requested file, but the next one does not.
Thanks for your help!
EDIT: The strange thing is that I've included classes in a class folder. And it included other files as it is supposed to, even though the paths are relative. WHY does this happen, and how can I fix it?
VERY IMPORTANT EDIT: I just realized that all this is because in my example, the inclusion in includes/phpFiles/processInput.php includes a file in the same directory: require_once("file in same dir.php"); This is the reason. If you are including a file with out specifying anything more than the filename, the include_path is actually the dir where the file the require is written in is in. Can anyone confirm this?
Use an absolute path.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");
Use a similar form for all your required files and they will work no matter where you are.
You can do this in a few ways, amongst others:
Use set_include_path to control the directories from where to perform require() calls.
Define a common absolute base path in a constant that you define in index.php and use that in every require() statement (e.g. require(BASEPATH . '/includes/template.php')).
Use relative paths everywhere and leverage dirname(__FILE__) or __DIR__ to turn them into absolute paths. For instance: require(__DIR__ . '/phpFiles/processInput.php');
By default, the current working directory is used in the include path; you can verify this by inspecting the output of get_include_path(). However, this is not relative to where the include() is made from; it's relative to the main executing script.
You're using relative paths. You need to use absolute paths: $_SERVER['DOCUMENT_ROOT'].
When you include/require, you are basically temporarily moving all code from one file, to another.
so if file1.php (which is located in root) contains:
require("folder/file.php");
and you include file1.php in file2.php (which is in a different location (say folder directory for example):
file2.php:
require("../file1.php");
Now all of file1.php code is in file2.php. So file2.php will look like this:
require("../file1.php");
require("folder/file.php");//but because file2.php is already in the `folder` directory, this path does not exist...
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
Your directory structure is off. The file inclusion is being seen from the file you're using it from. So, "template.php" is looking for an "includes/" folder in its current folder (/includes/).
As others are saying, use absolute paths, which will make sure you're always going at it from the file system root, or use:
require_once("phpFiles/processInput.php")
In your template.php file (which is far more likely to break if you ever move things around, which is why others all recommend using absolute paths from the file system root).
BTW, if you're using "index.php" as some kind of framework system, you can consider defining a variable that stores the address of common files such as:
define('APPLICATION_PATH', realpath(dirname(__FILE__));
define('PHPFILES_PATH', APPLICAITON_PATH . '/includes/phpFiles/');
I got a problem about including a included file in PHP.
Project
functions(folder) has a.php
xml(folder) has b.xml
index.php
This is my project structure(sorry about that, I can't post images).
I try to use "index.php" to include "a.php" while "a.php" is using "b.xml"
this is what i did on XAMPP and it works perfectly:
in index.php I wrote: include 'functions/a.php';
in a.php I wrote: $xml->load('xml/b.xml');
However if I copy these to my Uni's apache server, it can't open this b.xml.
This is not permission because when i change to absolute path it works...
Thank you guys in advance:-)
in a.php you should refer to ../xml/b.xml if you use include
thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
Try using set_include_path() to set the include path to your application's root directory; then you should be able to include files relative to this path.
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml'; )
If you can't do that, you should add xml's parent to your path.