PHP include fail to open path of included files - php

I'm trying to include file from another php framework but doing so it's giving me an error failing open the stream for the files which are included inside the file I'm trying to include.
Any idea on how to include it properly so that the files included inside my included file are able to be processed?

What Framework are you using?
I have little experience with templates but I don't think you should be adding PHP code to a template file.
Click Here for another question on how to add PHP code to a template file.
However as far as your path goes, try using:
include("../../other/index.php");

Your include only looks only one directory up.
include ("../../other/index.php");
would be two directories up so into the "admin" directory then the "other" directory
This has been asked before here

Related

Directory Mismatch while including other PHP files in it

I am working on a project (the first one) and got this error :
[I am NOOB and new]
First of all my directory is something like this: (Adding on main files)
[Yellow lines point to files and the purple one leads to a folder.]
I am working on the User Section right now.
I made a header folder and added a header.php file to it. which contains a header(): void. this header function basically has my navbar (few things are retrieved with DB). Also this same file is linked with .css and .js from AdminSection.
Now while making this function I was doing try and error on my user dashboard so it is now working 100% correctly as I wanted.
But when I included this function into another PHP file for let's say UserProfile it doesn't work anymore as all the included files in Header.php were working for UserDashboard.php (since it is in another directory which is working because of my try and error.)
I searched for a bit about this and found no solution but to make a header file for every directory which seems to be stupid.
Any solutions for it?
A Snippet from my header file if required
[The paths are working for Dashboard.php but not for files in another directory. I want a solution where I have only one header.php and it works for every file no matter its directory]

cannot directly call the php file inside folders using the directory path

I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.

PHP include directories

I'm trying to figure out how to get my php includes to work properly from files in different directories.
For example, I have a header.php for my layout which is included in the index.php - it's located in the root/ directory. Then, I have a page located at root/pages/etc/lib.php - It also needs to use the header.php. Now, obviously I could do a include "header.php" for the index and a include "../../header.php" for the lib.php file, but then I run into another issue...
My header.php also has a php include of it's own, and the relative path is relative to the original page (index.php or lib.php) and NOT the header.php.
Is the solution to simply give the full URL for the includes or is there another way to do this? Can I do URL for any included php files and it will work?
Most servers don't allow http wrappers in includes. You can do it with the document root:
$_SERVER['DOCUMENT_ROOT']
You can put it in a var and use it with every include. (Or define it)
One way to do it:
include $_SERVER['DOCUMENT_ROOT'].$pathToYourIncludeFileOnTheServer;
Another is to set the php include_path in the php.ini. If you include a file php looks in the directories specified in include_path if a file is matching the include and if so chooses that file.
Including directly via http://www.server.com/include.php is often a problem since allow_url_include is sometimes disabled in the php.ini if you are using a free webhosting or something comparable.

Issues while importing php files

I am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:
<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>
The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.
So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?
What is the reason behind impossibility of use relative path?
<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Set the base folder with all the files you want as an include path in php.ini.
That way, when you use include () or require (), it will automatically look in the included path.
www.php.net/manual/en/ini.core.php#ini.include-path

How to include php file to wordpress theme(index.php)

I would like to include cars.php file to index.php theme; the cars.php file is script, which loads data from the server and works as booking system, when I am trying to connect as usual way include('/.cars.php');, and check the source code in browser, its there, but no forms appear.
Could be the problem with wordpress engine itself?
I think you have a typo include('/.cars.php'); is likely include('./cars.php');
What you have now would look in the root directory for a file called .cars.php

Categories