File path for Require / Include PHP - 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

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

How to solve the fatal error in require_once

My project has two entry point
project (root-folder)
/config(folder)
config.php
/service(folder)
service.php
index.php
for example
File 1:/index.php ( first entry point) - here we include the config form the config folder
<?php
require_once('config/config.php');
require_once('service/service.php');
?>
File 2:service/service.php - here we include the config form the config folde
<?php
require_once('../config/config.php');
?>
if i call the File 2:service/service.php has no fatal error
but when i call the File 1:/index.php it became the fatal error as failed to require 'service/service.php' because it require again and config path is invalid
How to solve this issue.
Reason:
This issue arises because your execution starts from index.php and then you require service/service.php. Now in service.php when you do ../config/config.php, PHP tries to resolve the directory path from index.php point of view and it doesn't find any such file or directory. Hence, the error.
Solution:
Declare a constant in index.php. Check if this constant exists in service/service.php. If not, then require it, else skip it like below:
index.php:
<?php
define('INDEX_ENTRY_POINT',true);
require_once('config/config.php');
require_once('service/service.php');
?>
service.php:
<?php
if(!defined('INDEX_ENTRY_POINT')){
require_once('../config/config.php');
}
?>
Note: It is always better to use __DIR__ giving absolute paths than relative paths to avoid such issues.
You have to consider that when you call service.php from index.php, the root is that of index.php. Now there are many ways to get around this. You could decide that service.php is a main controller, just as is index.php, and thus belongs in the root folder. If for some reason you want to keep it as it is, then you have to define the root in order for it to adapt to the situation as in vivek_23 answer just above. Personally, i would keep service.php in the root folder, it is more logic.

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

How to include a php file in subdomain from main domain

I have two folders in my server:
--MainDomain
-header.php
-index.php
-footer.php
--Subdomain
-index.php
Now I want to include header.php from main domain in index.php which is in sub domain.
By using the include function I get the error
failed to open stream: No such file or directory in /home/content/xx/xxx/xxxx/xxx/
I know that this error occurs when a file does not exists on the given mentioned path, but how can I include a file from main domain to sub domain?
thank you in advance
I know this question was asked eight months ago, but I've just had this same exact problem and solved it. Hopefully this will help people in the future who stumble across the same thing.
For example, let's say you have two subdomains: carrot.cake.com and chocolate.cake.com
You have a file in the carrot subdomain: carrot.cake.com/scripts/login.php
And you have a file in the chocolate subdomain: chocolate.cake.com/index.php
You want to include the file "login.php" inside of the file "index.php".
The important part is that this IS possible but it's possible ONLY if you do store the files of both of these domains on one single server filesystem.
Add the following code to your index.php file:
<?php
echo '<br>'.$_SERVER['DOCUMENT_ROOT'].'<br>';
?>
And then go load up your index.php file in a browser. It will show you what your actual directory folders are. For example, when I did this, it showed me that my filesystem looked like:
/home3/cake/public_html/chocolate_domain
Yours will be DIFFERENT FROM MINE, so test that php code snippet out, yourself. And delete that code right afterwards. It is just for testing purposes. But now you know how to actually access all the folders to all your domains. This means that your index.php file is in, for example,
/home3/cake/public_html/chocolate_domain/index.php
and that your login.php file is in, for example,
/home3/cake/public_html/carrot_domain/scripts/login.php
Now, for the final result! If you want to include "login.php" inside of "index.php", then you should put the following code into your "index.php" file:
<?php
include("/home3/cake/public_html/carrot_domain/scripts/login.php");
?>
Please remember, once again, that the actual path for YOU is different, so test it out using the echo of the document root, like I said before~
good luck~
Here is a tidy way to do this.
An include from a subdomain:
include( $_SERVER['DOCUMENT_ROOT'] . "/../exampledomain.com/filetoinclude.php" );
This gets the whole path to your file, and then regardless of the subdomain you are in, it will jump up to the folder above that and call "exampledomain.com" domain. This saves showing your folder hierarchy to anyone who sees that file.
You could also reverse this
an include to a subdomain:
include( $_SERVER['DOCUMENT_ROOT'] . "/../sub.exampledomain.com/filetoinclude.php" );

Why doesn't this require_once statement find the file? (PHP)

I not sure what's going on..maybe I missed something simple.
In my connectvars.php file, I connect to the database using the variables in my config.php folder.
Here's the hierarchy:
admin(folder)
config.php
includes(folder)
connectvars.php
index.php
I want to get information from config.php to use in connectvars.php, so I use:
require_once("../admin/config.php");
But everytime I do this I get Warning: require_once(../admin/config.php) [function.require-once]: failed to open stream: No such file or directory in /home/a8879415/public_html/includes/connectvars.php on line 2
BUT when I type: require_once("admin/config.php");, it works.
I thought I had to go up a level, then go down to admin, then get config.php. So how come I just need to go into the admin folder then get config.php?
Are you running includes/connectvars.php directly or is it included by other file?
I think you are running index.php and every files are included relatively to it's path.
If you want to use relative path to other files, it can be done like that:
require_once( dirname(__FILE__) . '/../admin/config.php' );
They run in the context of the main script, so file references should originate from the main script's directory.

Categories