Let say, i have a file product/vps.php and i want to include it in root folder index.php. i can simply use include "product/vps.php"; in index.php to include content from product/vps.php.
But the problem is when i load index.php the page is broken because product/vps.php have lot of CSS, Image and Javascript file from product/assest/xx.
How can i write a include in PHP which call content and and also include the sub-content of content file.
Edited :
It possible with defining the root folder with help of dirname(__FILE__) or __DIR__ or DIR or include_once(ROOT .'product/vps.php');
Related
I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.
I've designed header in Adobe Muse and I want to include it on my wordpress theme. I tried to use a php include statement like so within the header.php file:
<?php
chdir('pages');
set_include_path('pages');
include 'index.html';
?>
The code calls the correct file but none of the images or css is loading. When I look at the debugging console it shows that the code is not calling the correct path for these images and css. It's looking in the root directory instead of the pages directory. How can I get the index.html file to run with the correct working directory?
cant figure out how to do what im trying.
I want a way to include PHP files so that i can call the same file from within any file in any level folder and it always points to the same files.
So i have a website setup e.g.
root
init.php
databaseconn.php
index.php
/css/
main.css
/settings/
user_profile.php
/template/
sidebar.php
/includes/
get_user.php
For example, on user_profile.php i am including the sidebar.php file.
For now i would use:
../template/sidebar.php
And then ../includes/get_user.php inside that sidebar file.
Which works fine. The problem however is that i also include the same sidebar.php file on my index.php page. This then causes the issues.
To counter this, i have tried this:
<?php
$isDevelopment = true;
$baseInclude = ($isDevelopment ? '//localhost/website/' : '//www.site.com/');
?>
And then when including any files across the site i have used:
include $baseInclude. 'template/sidebar.php';
When doing this however i am getting an error.
The error shows:
Warning: include(//localhost/website/template/sidebar.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/website/settings/user_profile.php on line 46
I dont understand why its looking for the file inside the user_profile.php file when it shows and i have defined that its coing from the localhost path...
Any ideas?
in your init.php file add this at the top
define('ROOT', __DIR__);
now in your other files use
include (ROOT . '/full/path/to/file.php');
in your code it would look like this
include (ROOT . '/template/sidebar.php');
One possible solution:
$baseInclude = getcwd();
I'm using a PHP include for my header/navigation, but some pages are unable to find that file. I'm using a root-level link
<?php include("/includes/masthead.php"); ?>
but pages outside of the root folder are not locating the masthead file. For instance:
index.php locates and processes masthead.php just fine;
adopt/adoptadog/php returns an error saying the file does not exist.
Is this because PHP doesn't process links in the same way that HTML does, so my root-relative link just isn't being interpreted by the php include function?
I'd like to be able to have a root-relative link work in my include statement so that statement can go into an Expression Web template. The template seems to write the same address in every page regardless of the location of the page. Maybe it doesn't see a link within a PHP tag the same way it does in HTML--I don't know.
I hope this is clear. Any help?
You could use the document root as the anchor:
include $_SERVER['DOCUMENT_ROOT'] . '/includes/masthead.php';
If your included files are one level outside of the document root, you just need to move with it:
include dirname($_SERVER['DOCUMENT_ROOT']) . '/includes/masthead.php';
If you have a script that gets loaded by all your pages that resides on the document root itself, you can use a constant:
define('PROJECTDIR', dirname(__FILE__));
To include the mast head:
include PROJECTDIR . '/includes/masthead.php';
Try putting this at the top of your script:
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
Or go to your php.ini file and update the include_path directive to always search your webroot directory.
I have made a config file that includes te database information (connection, and more).
The config file is called by index.php. So I have this:
index.php->calls->config.php->calls->db.php
This works, but sometimes the config.php is not called by the index.php but by for example header.php. The problem is that the relative path in the config.php to the db.php doesn't work.
How can I solve that? Should I use absolute paths?
Thanks!
If all the files are located in the same directory, you can include it like this:
include __DIR__."/config.php";
Otherwise create a constant in your index.php page before all includes that define your application path, and use that to include your files, E.G:
in index.php:
define('APP', __DIR__);
include APP.'/config/config.php';
in config.php:
include APP.'/lib/db.php';