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");
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 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!
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 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.
Okay so on my website I have a scripts folder which includes a php file which connects to mysql server so If I move a database then It will change it on all the files which are connect to the database.
I also have another folder called templates. In that folder there is a top for the header and the footer. In the header template I wrote:
require("../scripts/connect.php");
And I have another folder called, category. And that folder includes the header and the header includes connect. But then it displays and error that there is no such files.
Please help. Thank you
A good practice is to include a main config in all running php files, usually called config.php :)
in this config file create a constant called SITE_ROOT or something similar that point to the exact folder like this
define("SITE_ROOT", "/var/www/mysite");
Then on any include, include_once, require, require_once use it like this:
require(SITE_ROOT."/scripts/connect.php");
This should solve any relative path drama
You shouldn't use relative paths with the include/require, but use a constant defining the ROOT_PATH of your website.
Example:
In all the files calling needing includes:
define(ROOT_PATH, '../');
include ROOT_PATH . '/scripts/connect.php';
And in /scripts/connect.php (and all the other files that will be included somewhere), all the includes should use ROOT_PATH (without defining it).