I am new to php and facing initial problems :).
My directory structure is as follows :
site
index
homepagesections.php
assets
css
style.css
config
db.php
include
header.php
index.php
In the index.php , which is in the root folder I need to include the header.php file which includes config/db.php and also assets/css/style.css.
I have tried all the options ( including. the DIR option ) but failed. What is the best way to include these files and also the path for the style.css ?
In index.php path to header is include/header.php:
include('include/header.php');
In header your path to db.php is ../config/db.php so you will have:
include('../config/db.php');
Note '../' - this is how you navigate 1 level up, from the directory your file is. In header adding css will be something like:
<link rel="stylesheet" href="../assets/css/style.css">
In index.php:
include (__DIR__.'/include/header.php');
In header.php:
include (__DIR__.'/../config/db.php');
include (__DIR__.'/../assets/css/style.css');
Related
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');
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.
This is my directory:
Wamp>www>Newlinks
CSS (folder)
header.php
footer.php
Profiles (folder)
MainPHPfile.php
Now, I want to include the header and footer files into MainPHPfile.php, but it's not working. How to set the path so that it works?
Same problem is with CSS. I want to use CSS files from CSS folder for those header and footer files. Please solve this problem. I shall be thankful to you.
You could go a directory up with .. like this:
<?php
include '../header.php';
include '../footer.php';
Same with CSS.
In order to go up a folder, use the ../ relative path, if you need to go down a folder, just writte the relative continuous path. example/another_folder/file.php
You can use ../path/folder/to/file.ext to go to the previous path. /path/folder/to/file.ext to go to the root of your website e.g /public-html/ or /htdocs/ .
I have the following file structure on my PHP localhost:
Root
css/
main.css
admin/
admin.php
index.php
header.php
Both admin.php and index.php are requiring header.php, in which main.css is included like the following:
<link rel="stylesheet" type="text/css"
href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/main.css" />
However, the css file isn't included properly. When I look at the Source Code in Chrome on Mac, The browser has turned that into
http://localhost/Users/ljhljh235/Documents/web/hetd/css/main.css
in which http://localhost is not intended to be here. Could anybody help me on how to build the correct absolute path for the css file?
P.S. My way of including header.php in admin.php and index.php is
require_once ($_SERVER['DOCUMENT_ROOT'].'/header.php');
And I'm using MAMP 2.1.1.
Thanks for any help.
The variable $_SERVER['DOCUMENT_ROOT'] is for server side path, use /css/main.css only, it will be relative to the domain ie : http://domain.com/css/main.css
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';