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";
?>
Related
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 clean up my website folder structure. I have too many processing scripts in the root directory. The reason is because they won't work if I move them to a sub folder and call them from there.
For eg.
root directory
- images folder
- css folder
- js folder
- includes folder
- admin folder
index.php
ajax.php
Say I move the ajax.php to includes folder, it will look like this.
root directory
- images folder
- css folder
- js folder
- includes folder
- ajax.php
- core folder
- template folder
index.php
Now on every page I have an init.php required. In ajax.php for eg.
<?php require_once 'core/init.php'; ?>
// rest of the code
The issue I am having is that the init.php file won't run as long as ajax.php is in includes folder. I get an error something like this.
Warning: require_once(core/init.php): failed to open stream:
If i move the ajax.php back to the root directory, it'll work fine.
On index.php, this is how I am calling the ajax.php
core/init.php works fine when requiring in the "templates" folder.
Perhaps someone can tell me the solution to this?
Try using PHP's chdir function. This will change the working directory that your PHP environment is working in, and therefore the relative paths that your include functions are looking in. Ideally, you'd want to call chdir with the root of your project, so that when you do require_once('core/init.php'); you don't end up calling from the core directory, but rather your . directory.
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" );
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
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");