i am trying to include the same connect.php file as a part of the nav.php file throughout multiple different directories and directory depth's with a generic 'dynamic' substitute for the include path back to the connect.php file in the website root folder eg.
www.mywebsite.com/shop/shoes/sandals/index.php
and
www.mywebsite.com/shop/shoes/sandals/index.php
etc.
with the connect.php file being located in the root directory
www.mywebsite.com/connect.php
the code I am currently using in the nav.php (which is included in all pages and directories that are accessible by users)is as follows.
<?php
include '../../connect.php';
?>
however this will obviously not work in directory paths like
www.mywebsite.com/shop/shoes/sandals/index.php
I have no previous experience making a single connect.php file accessible by multiple directory paths using an absolute path with php.
however some guidance on this matter would be extremely helpful.
Thanks
You could try using something like
include $_SERVER['DOCUMENT_ROOT'] . "/connect.php";
To continue reading...
Hope this helps!
Related
So I have a web application hosted on a site. The web root (I.E. the files the client can access) is the public_html folder. However, I need to include files outside of the public_html folder. I do this using php include. I get an error no such file or directory. When it shows me the path it is still looking in the public_html folder, which is not where I need it looking.
The code looks like this:
<?php include('../eCommerceCore/shoppingCart.php');?>
I need it to look up one level but it will not search outside of public_html. Also, the file containing the line of code shown above is in the public_html folder if that helps.
Sometimes the current directory isn't what you expect it to be, such as when you include a file from an included file.
I like to use $_SERVER['DOCUMENT_ROOT'] on my includes so that I can always reference them absolutely from the root of my site:
<?php
include($_SERVER['DOCUMENT_ROOT']."../eCommerceCore/shoppingCart.php");
?>
Or this way you can try
<?php
include "../eCommerceCore/shoppingCart.php";
?>
If your includes directory is above your document root, you can use .. to still reference from the root.
I'm having a problem with the folowing:
I have a file called config.php, which I include in two other pages: login.php and index.php. The pages are organized as follows:
index.php
Config/config.php
Page/Users/Login/login.php
lib/smarty.php
My problem, is that, within config.php, I have another include to a different file: smarty.php.
But because index.php and login.php are not on the same level, the smarty file only works for one of them:
Example : if I have, in config.php:
include('lib/smarty.php')
then it only works for index.php
if I have include('../../../lib/smarty.php')
then it only works for login.php
Is there a way for me to use the same config.php for both login.php and index.php and have smarty.php work for both?
I hope I was clear, and thank you in advance
NOTE: The problem is not how to include files from different folders into another, it is to include one file into two different pages in different directories and with different paths.
Use dirname(__FILE__), or if available, __DIR__ (PHP >= 5.3). They resolve to the full path of the directory the current file is in. It is better than explicitly using the current absolute path to the file because it allows you to move the files in the file system (as long as the files stay in the same location, relative to each other).
You'll have to change Config/config.php to include lib/smarty.php like so:
include(dirname(__FILE__) . '/../lib/smarty.php');
While you're at it, you should probably change your other include/require to use a similar construct.
Use the absolute path in all of your includes.
like:
include '/var/www/html/Config/config.php';
I am trying to get my include() functions in order but everytime I switch the syntax in one directory it messes up its subdirectory and vice versa.
I have a file called 'header.php' in my 'localhost/FTS/includes/header.php' folder.
The 'FTS' folder has my index.php file so it is technically my root folder while I am testing.
In the file 'localhost/FTS/admin.php' I use the line include 'includes/header.php'; and it works fine but then when I go into the file 'localhost/FTS/admin/members.php' the include file is not found. Also inside of my 'header.php' file I include a couple more files from my root directory.
I just want all of my includes to work from each directory. Any ideas?
The include statement includes files relative to the script executing include, which is why you're seeing this issue. You have a couple of options available to you:
You can preface your include path with something like $_SERVER['DOCUMENT_ROOT'], which if you're using Apache, will reference your document root directory. So something like include( $_SERVER['DOCUMENT_ROOT'] . "/includes/header.php" ); will appear to the script as an absolute path, so it will work if called from various places in your structure, but still be portable (note that it would require being called from a web server, this won't work if you're using your scripts through CLI).
You can create some sort of placeholder at the root of your directory structure, and work your way up until you find it, then consider this the root and make your include statements relative to this. This will be more portable and work in CLI mode, but it will be slightly more resource intensive.
You can use different include statements depending on where you're script including the include file is located, such as include( "includes/header.php" ); if the file you're including is in the includes directory of the directory you're currently in, or include( "../includes/header.php" ); if the includes directory is in the parent directory of the script being run.
This is a big confusing but I'll try to be as clear as possible.
I am using the Starter Site PHP Template in Web Matrix for an assessmnent, now when I move several files such as the files containing the database details, header, footer ect.. into the admin file I am having an issue with my index.php and page.php.
These files use require_once() to include several files within the admin folder. However in the admin folder the files being required have require_once tags again within that directory.
When I use require_once('admin/database.php'); the database file has require_once(somefile.php) within it. There are numerous files like this being included.
How can I use include these files using require_once in the root directory without getting errors like these.
warrning: require_once(/Includes/simplecms-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\starter-ste\admin\Includes\connectDB.php on line 2
The includes folder is located within the /admin/ folder.
php require statements will look for files in the current directory and directories in the php_include_path. You can solve your problem by adding your include directories to include path.
You can do this dynamically by calling set_include_path
$directories = array('/library', 'var/', get_include_path());
set_include_path(implode(PATH_SEPARATOR, $directories));
[EDIT]
Your server might not know where the root directory of the website is. So, first, get the document's root directory and append it to the file name like this:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require_once "$root/your_file_path_here";
Looks like you haven't enclosed the file name within quotes. So that
line should probably be like this:
require_once("/Includes/simplecms-config.php");
Hi I'm new to putting up websites. I have a document root: home/user/public_html/www.example.com/public where all of my static file should go to be publiclly viewable. But many of these files include config.php. These config.php files have user names and passwords so it just doesn't seem right to put them in the public folder.
Where should these files go?
If I move the configs to a different directory (I have tried) then the static files can't find the config files anymore. Would I point to the new location?
Yup you need change the location of config.php with respect to the static files. If I understood correctly, by default they both are in same directory, so you must have include 'config.php'; if you want to move them into a new folder, suppose name of the folder is hidden, then you have to change it to :
include 'hidden/config.php';
Finally if you decided to move up one directory, you have to use include '../config.php';
You can read in detail about relative paths here:
Absolute vs relative links
Well, WordPress, one of the more popular and largest platforms out there just puts a wp-config.php file into the document root, so if it's good enough for them you could say it's good enough for you :)
You can of course store the config.php file 1 level up from your actual document root. PHP can still access it if given the correct path.
E.g. http://codex.wordpress.org/Hardening_WordPress#Securing_wp-config.php
Each file that includes config.php needs to reference it with the correct path. If you just store the config.php in the root you can change all the includes to use
include_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
This will work no matter the location of the including file.