Include statement in php file - php

I have a php file (php1.php). Within that php file i have the following line:
include('php/PROTECT/login.php')
However when i load the page i get the following error:
Warning: include(php/login.php) [function.include]: failed to open stream: No such file or directory
it just totally ignores the /PROTECT/ section?
Does anyone have any ideas why this is and how i can resolve the issue?
My file structure is as follows: (php1.php is within /php and login.php is within /php/PROTECT

It should look like:
include('php/PROTECT/login.php');

Make absolutely sure that that's the include call that's throwing the error. Make sure that no other code, in php1.php or in any file it has included or required tries to include php/login.php.

If php1.php is within /php/ (which I understand it to be), then you will need to use the following:
include('PROTECT/login.php');

Includes in includes can be a bit messy, but if you concatenate with DIR it is easier. DIR is always path to the file it self, if you use DIR you can always use the relative path. Like this:
include DIR . "/PROTECT/login.php";

As you can see by the error message:
Warning: include(php/login.php)
"PROTECT" is excluded from the path, probably for being in all caps.
Change the directory to all lowercase ("protect" or similar) and see if you get the same error.

Related

PHP use of define and __DIR__ explanation

I have been inspecting code, reading questions on StackOverflow, but I simply don't get it, or rather I dont understand the explanations and / or logic behind it.
Consider The Following
If I have a directory structure like this
Now I want to set the head.php file to be globally accessible throughout the application (just as an example)
define('Head', __DIR__ .'/views/head.php');
If I do the above, I get the following result:
C:\xampp\htdocs\carRental/views/head.php"
Which is technically what I want,, however, notice the URL contains forward and backslashes?
Can I get access to the head.php file by calling Head anywhere in my directory tree?
Im sorry, Ive been inspecting code and read the manual and questions on here, if anyone could give a rookie a clear explanation it would be greatly appreciated.
UPDATE:
When I try to do the following in landingPage.php I get the following errors
include_once Head;
Notice: Use of undefined constant Head - assumed 'Head' in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(Head): failed to open stream: No such file or
directory in C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(): Failed opening 'Head' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
When you used define('Head', __DIR__ .'/views/head.php'); you have hardcoded the slashes in the definition.
However windows by default uses \ as the default directory separator so __DIR__ will be using \ in the path when in Windows (it's ok with using / as an alternative one though so it shouldn't be a problem).
You can do the following if you want them to be consistent:
define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php");
Which is technically what I want,, however, notice the URL contains forward and backslashes?
yes
Can I get access to the head.php file by calling Head anywhere in my directory tree?
no
The mix of foreward and backward slashes is created by your command
__DIR__ creates C:\xampp\htdocs\carRental and '/views/head.php' is the string you append.
To be able to use your defined HEAD you would have to load the php file which defines it. another php files does not know what this file does or doesn't do as long as it is not persisted. (which you don't do in the code provided)
To load a file and make your definition available use include_once / require_once
__DIR__ will always resolve to the absolute directory of the file using it.
The reason for forward and backslashes. This part:
C:\xampp\htdocs\
Is Windows file path.
This part:
carRental/views/head.php
Is the webserver path, i.e not Windows.
Your define will hold the correct file path so try to include it now:
include_once Head;

Error inserting a .php file using the require directive

I am pretty new in PHP and I have the following problem trying to define a required file into a .php file, I have done in this way:
require("../../common/define.php");
because the define.php is into the common/ two level back related to the file in wich I am instering this require directive.
But this syntax seems to be wrong because it give me this error message:
Warning: require(../../common/define.php): failed to open stream: No such file or directory in C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php on line 3
Why? What is wrong? How can I insert a required file that is some level back in the tree?
PHP current file:
C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php
So then in this file you have a request for ../../common/define.php
Which is two steps [folders] back, then into the common folder so;
This is two steps back:
C:\xampp\htdocs\PandaOk\templates\
And into the common folder:
C:\xampp\htdocs\PandaOk\templates\common\
So what you've given is a directory that doesn't exist, which is exactly what the error tells you.
instead, only go one step back:
require("../common/define.php");
Alternatively, and far better practise, is to do an absolute file path using $_SERVER['DOCUMENT_ROOT'] such as something like:
require $_SERVER['DOCUMENT_ROOT']."/templates/default/common/remove-booking_pc.php";
This would mean that the reqire always succeeds regardless of where in your project diretory tree it is called from.
(I don't know what directory your document_root will be defined as, but you should get the idea, I hope)
You can also give a full static file path such as commented by Sahil Gulati.
require "C:\xampp\htdocs\PandaOk\templates\default\common\define.php";
P.S: Require and include do not need brackets.

Warning: include(../includes/db.php): failed to open stream:

This is the problem I am facing now Can you please suggest me to get rid of this problem. Before I have not get this problem It was working nicely now I don't know what happened the path is correct only. Anyone please solve it...
Warning: include(../includes/db.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 3
Warning: include(): Failed opening '../includes/db.php' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 3
Warning: include(functions.php): failed to open stream: No such file
or directory in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 4
Warning: include(): Failed opening 'functions.php' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 4
Based on the error posted, it seems to be that the include_path is referring to the path (include_path='C:\xampp\php\PEAR') while the PHP files are included in some other folder.
You need to update the include_path variable to point to the correct location. You can find this in the php.ini file.
Computers don't lie, if it says it doesn't see it, then it is not there
Make sure you are making it look at the right place, and if I will advise you do it manually also to confirm
I also suspect it is an issue arising from using relative paths. Please make sure the path is right considering where you are currently calling the file from
e.g calling include(../includes/db.php) from index.php is different from calling it from admin/index.php
EDIT
If your are calling from C:\xampp\htdocs\portfolio\admin\includes\admin_header.php to C:\xampp\htdocs\portfolio\includes
then
include(../../includes/db.php);
is the right syntax to use
Hope this Helps
I have had this the same issue before and it ended up being something dumb on my part i forgot to start the MySQL on the XAMPP control panel. I'm sure i'm late the the party on the answer but hope it can help someone later down the road.
a bit late but i just reply for those who might get the same error .
assuming you are storing header.php in your includes directory and its not beside your index.php,so when you include bd.php into your header.php and then including your header.php into your index.php pay attention that apache knows index.php direcotroy as your pwd so if you have set that relative path for db.php based on header.php directory its not gonna work.you can use absolute path to avoid this or just set you relative path based on relative location of db.php from index.php
It look likes, the files you are trying to include in your php codes, are not on that place. Try to save all your include files in a particular folder and then call it wherever you want to call it.
suppose your index.php file is in xampp/htdocs/your_folder_name/index.php , then simply create a folder name whatever you want to name it inside your folder name your_folder_name/your_new_folder and save all your files which you want to include, as if you are saving db.php then call it on the top of your page as:
<?php include("your_new_folder/db.php");?>
This has to do with how you are coding your relative path.
I am assuming you got this error when your app made reference to admin_header.php, which has an include() to try and call {document_root}/porfolio/admin/includes/db.php. My educated guess is it contains your implementation of the connection string to the database.
The correct relative path should be: include('../../includes/db.php');
This link should give you a better understanding of relative paths (and paths in general).
https://phpdelusions.net/articles/paths
In case this helps anyone else, I had this problem and then realised WAMP had crashed (or maybe I didn't start it). Either way, WAMP wasn't running on my local machine, so... doh.
This should be the very first thing you check (in line with, "is it plugged in?").
Try removing relative paths ("../")
e.g. change include(../includes/db.php) to include(includes/db.php)

PHP is looking for a file in the wrong place

I am an amateur web developer and I am trying to get my site live for the first time. The site is working and all of the files are uploaded but the site is not loading my PHP includes.
Here is the error message:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home4/myUsername/public_html/index.php on line 3
How can I get PHP to look in public_html/ rather than public_html/index.php?
EDIT: I have tried editing the include path. It doesn't seem to change where php is looking for the file. Additionally my includes work properly in localhost.
I'm going to assume this is your folder structure:
public_html/index.php
public_html/includes/header.php
Generally (not always), $_SERVER['DOCUMENT_ROOT'] will now reflect the path to the base public_html directory (this I'm assuming based on the context of your message). This means you can always point to the root this way. - no matter if you have /index.php or /my/deep/file/structure.php
Try this with your include statement on index.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
You may need to change the include path in your php.ini file or use set_include_path() to change the include path.
Here is the manual entry for the function call if you'd like to read more about it.
Have you checked already the include file?
in given. include(folder_includes/file_header.php);

Why do I get a Warning and a Fatal error when I use ../?

When I use ../mysqlConnect.php I get the following messages.
Warning: require_once(../mysqlConnect.php) [function.require-once]:
failed to open stream: No such file or directory in /home/content/etc...
Fatal error: require_once() [function.require]: Failed opening required
'../mysqlConnect.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/etc...
When I use the directory name - mydir/mysqlConnect.php - everything works fine.
require_once('../mysqlConnect.php') asks PHP to look in the directory above the one your script is currently in for mysqlConnect.php.
Since your connection file appears to be in a mydir directory, require_once('mydir/mysqlConnect.php') works because it looks in that directory, which is contained by the one it's currently in.
Visual representation (assuming script.php is your script including that file):
dir/
subdir/ # PHP looks here for ../mysqlConnect.php
script.php
mydir/ # PHP looks here for mydir/mysqlConnect.php
mysqlConnect.php
Require is relative to the invoced script, not the script you call require() in. Use something like this to have an absolute path:
require(dirname(__FILE__) . '/../mysqlConnect.php');
In PHP 5 you can also use DIR.
because it doesn't find your file then. to give a more specific answer I need to see you file-/folder-structure
That's because you are not specifying the correct include path. ../ refers to parent directory. ../../ goes two directories back, ../../../ goes three of them back. If the mysqlConnect.php file is present in the same folder as your script, you don't need to specify ../ in the include.
Make sure that you specify the correct path. You can easily check whether or not you are specifying correct path like:
if (file_exists('../mysqlConnect.php'))
{
echo 'Iam specifying the correct path !!';
}
else
{
echo 'Well, I am not :(';
}

Categories