Issues with defining a path for PHP include across multiple folders - php

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

Related

How to include files from another folder?

I don't really know how to describe my problem, so maybe I will present it graphically.
I've got folders of my site like:
1. Project folder
a. folder "components"
head.inc.php
navbar.inc.php - there is php include_once('class/functions.php');
content.inc.php
footer.inc.php
b. folder "class"
dbconfig.php
functions.php there is php include_once('dbconfig.php');
c. folder (which i want to create) called "tutorials"
tutorial.php where is on header:
include('../components/head.inc.php');
include('../components/navbar.inc.php');
also in main project folder got files like
index.php - where included all *.inc.php from folder "components"
and some other files .php
I have no idea because I want to create a folder called "tutorials" and create a file called "tutorial.php" and include components from main project folder (i mean head.inc.php, navbar.inc.php etc.)
The problem is when i put "include" of this components, it shows me error like
"Warning: include(components/head.inc.php): failed to open stream: No
such file or directory "
How to Include?
<?php
include "components/head.inc.php";
?>
Include Not working
Sometimes in php include_once won't work but include will, So try require / require_once / include / include_one
<?php
include "components/head.inc.php";
include_once "components/head.inc.php";
require "components/head.inc.php";
require_once "components/head.inc.php";
?>
It's not like This is the only way to check, but if you see include_once helped you go do some research
None of this is working
When going into the console tab of chrome / firefox / edge / brave and ... You can see a URL when you click on it it shows you an 404 Page so check the url and if needed you need to go back a directory like so:
URL : mywebsite.com/main.php/users/file/profile.php
The url we want : mywebsite.com/main.php/users/profile.php
How we include it
<?php
include "../profile.php";
or
incude "../../ to go back two directories";
?>

Include PHP file from parent sibling folder in word press

I try to include some PHP file to another one by using include function.
It works fine when files are at the same directory that simply can do
include (file.php)
and if a file is in the child folder like
include (folder/file.php).
but I want to make my root WordPress folder project cleaner then change the location of my template files to pages-template folder but unfortunately, I can't include files from another folder that are the sibling with pages-template.
I try this
include '../inc/package-save.php';
but I got errors
Warning: include(../inc/package-save.php): failed to open stream: No such file or directory
any trick to fix this.
Finally, I found the solution.
just put the dirname(__FILE__) function before the directory that I want to include.
This function will get the directory path that files that I write this function to it.
for example, if file be at C:\xampp\htdocs\amt-master\wp-content\themes\wp-bootstrap-child it returns this path
But as my case, I want to get the previous path to do that I should just put this function in the same function like this
include dirname(dirname(__FILE__))."/filename.php"
then it will return this path
C:\xampp\htdocs\amt-master\wp-content\themes
then you can write remaining path.
Try to use the following
include TEMPLATEPATH . '/inc/package-save.php';

php include path issues in directory

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

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

require_once files from another directory requiring within

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

Categories