php include path issues in directory - php

Happy new year everyone... I have been working on a project and my file structure is
index.php
functions
profile
includes
main forlder is "mysite"
I have a header and footer if I include into the index.php its fine if I include in profile/index.php I get and error
I have tried to use
include($_SERVER['DOCUMENT_ROOT'] . '/header.php');
but have had not success still errors
the error I get when trying to go to profile/index.php
Warning: include(C:/Program Files (x86)/Zend/Apache2/htdocs/header.php): failed to open stream: No such file or directory in C:\Program Files (x86)\Zend\Apache2\htdocs\mysite\profile\index.php on line 2
if you look at the top warning its trying to include from htdocs however the site main folder is mysite

Your main folder is mysite, so add mysite after $_SERVER['DOCUMENT_ROOT'] for profile/index.php the code will be like this:
include($_SERVER['DOCUMENT_ROOT'] . '/mysite/includes/header.php');

Related

Php Error: include_once(): Failed opening 'XXXXXXX' for inclusion (include_path='XXXXX') in XXXXX on line 7

I am here because my php code keeps giving me this annoying error whenever i include something from a directory , or just including something from a file. The error is originating from includes.php. I later found that you have to add the COMPLETE path to the directory. So I did, but it just keeps giving me the same error.
My code:
include_once (__DIR__."/inc/defines.inc.php");
Note that __DIR__ gets replaced by the files own directory path. The issue here is most likely that the path from the script you include does not match the directory you want to include a file from.
A common practice is to have a file at the root of your project that defines the root path. If you include that config file (or whatever you call it), it is easier for you to include other files.
For example. imagine a project like this:
config.php
foo/bar.php
foo/baz/bat.php
Example content of config.php:
<?php
define('ROOT_PATH', __DIR__);
Now, the content of foo/baz/bat.php could be:
<?php
// Get the config file
include '../../config.php';
// Include the content of foo/bar.php
include ROOT_PATH . '/foo/bar.php';

including a PHP file from localhost gives warning

i have an index file with PHP code in localhost/KK/admin/pages
<?php
require_once 'url.php';
include 'localhost/KK/admin/pages/sidebar.php';
?>
url.php is within the same folder as index.php file. sidebar.php is in localhost/KK/admin/pages/admin/pages.
url.php has the code
<?php
$url = "localhost/KK";
?>
when i open index.php it gives me
Warning: include(localhost/KK/admin/pages/sidebar.php): failed to open
stream: No such file or directory in
C:\wamp64\www\KK\admin\pages\index.php
and
Warning: include(): Failed opening
'localhost/KK/admin/pages/sidebar.php' for inclusion
(include_path='.;C:\php\pear') in
C:\wamp64\www\KK\admin\pages\index.php
can anyone explain why i am getting these errors and why i cant call sidebar.php? also a possible solution?
Let's traverse your directory: localhost/KK/admin/pages/sidebar.php
Your script file is located here: C:\wamp64\www\KK\admin\pages\ or KK\admin\pages\
From KK\admin\pages\ you ask for a directory (thats right, a directory not your server) localhost, from localhost to KK to admin to pages.
include 'sidebar.php'; will do fine as the script is located in KK\admin\pages\ and your include as well so you can include the file relative to the location of index.php located in C:\wamp64\www\KK\admin\pages\
Instead use the relative path, ..\ to go back 1 directory or use a constant to keep track of your root.
<?php
const ROOT = 'C:\wamp64\www\';
include ROOT . 'folder\file.php';
?>

Warning: require_once(../core/Config.inc): failed to open stream: No such file or directory

I have a question about require_once in php.
First this my folder structure:
monsite.dev
classes
Database.inc
Users.inc
UsersProfile.inc
core
Config.inc
web
Index.php
tester.php
Database.inc to connect to my database
web/Index.php for home page
tester.php just for testing thing
I use require_once to link file together, which works almost very good except for the tester.php, which keeps telling me "failed to open the stream".
I tried 2 different way in tester.php:
1 - require_once '../classes/database.inc'; (failed to open Database.inc)
2 - require_once 'Users/Nicks/Sites/monsite/classes/database.inc';(failed to open Config.inc)
But when I use it from index.php (require_once ../classes/database.inc) it works perfectly.
Why is this not working from tester.php while it works from index.php?
Many thanks for any help.
Nic
require_once 'classes/Database.inc';
You can use this because file is on same directory.
That is because that is the incorrect path. In tester.php it should be:
require_once __DIR__ . "/classes/Database.inc";
Using ../ goes back up a level. Therefore in your directory structure, you're saying that the classes/ directory is alongside the monsite.dev directory. Not inside it.
When used in index.php, which is in a subdirectory, you have to go back up a level - which is why it works from there.
tester.php & classes directory is on same folder so no need of ../. Try with -
require_once 'classes/Database.inc';

Issues with defining a path for PHP include across multiple folders

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();

File path for Require / Include PHP

I know this might have been asked before but I can't figure out after reading them posts whats best for my situation. Sorry.
I'm having difficulties in declaring a file path for my include / require_once files.
My folder structure is like this.
Root folder:
index.php
Folder(core)
init.php
Folder(user)
registration.php
login.php
logout.php
....
Folder(functions)
functions.php
Folder(inc)
header.php
footer.php
So the problem I'm having is that every time I try to include or require a file lets say from Folder(core) --> file init.php in Folder(user) -> File registration.php (registration.php would be where the include command be located) it throws me an error and page doesnt load. I get an error "Warning: require_once(/core/init.php): failed to open stream: No such file or directory". Why is this happening? would be same if I try to include other files from different directories.
Please help, I know this might be a silly question but I can't figure out
If your index.php is always loaded first and loads the other files that may also load other files, then everything is relative to the root directory:
core/init.php

Categories