Error inserting a .php file using the require directive - php

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.

Related

Failed opening required file from other file

So I have domain.be/index.php
Then I have require Controllers/indexController.php;
In that file I have require ../Model/opalus.php;
So I builded the first controller, for my framework which worked, yèeey.
But If I use my controller in the index.php I get
PHP Fatal error: require(): Failed opening required '../Model/opalus.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/share/pear') in /home/webkeuke/public_html/Controllers/indexController.php on line 2
I have tried using _DIR__ no ../ or ./ etc, can somebody please fix this?
Sometimes multiply file inclusions by relative Paths (../, ./) in PHP on complex projects can make a lot of problems especially in error tracing.
To prevent your project and yourself to stuck in this problem I recommend to do all file inclusions with fullpaths (CHANGE include("../inc/extra.php" - TO include("/var/www/myproject/inc/extra.php").
But this method can also be make a lot of work if your filesystem paths changes (f.e. when you change your Server environment or the directory then you have to change all files with an include in it).
So the best solution is to define the basic filesystem path in a global variable at a point where your scripting starts. Then use the defined basic filesystem path variable on every include();
Example for your case (index.php):
<?php
define ("FSPATH","/home/webkeuke/public_html/");
[your code]
// include /home/webkeuke/public_html/Controllers/indexController.php
include (FSPATH."Controllers/indexController.php");
[your code]
?>
Example for your case (Controllers/indexController.php):
<?php
[your code]
// include /home/webkeuke/public_html/Model/opalus.php
include (FSPATH."Model/opalus.php");
[your code]
?>
Ths solution should prevent you to get stucked at a point with wrong include Paths.

../ on URL on php not working

this is kind of a silly question, but as I can't sort it out I thought it might be good to get some help. The point is that the ".. /" to go back directory is not working.
The file I'm executing is in a folder that's on the main route and I need to go back to the main route and then enter another folder to load this other PHP file but it's not working what could be causing this issue.
ERRORS:
Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3
Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3
DIRECTORY STRUCTURE:
File where the requiere once is:
/public_html/classes/filethatwantstoacces.php
File where it wants to get:
/public_html/PHPMailer/PHPMailerAutoload.php
require_once('../PHPMailer/PHPMailerAutoload.php');
What you should be using is the $_SERVER['DOCUMENT_ROOT'] variable. Please read this answer to another question for details.
If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).
so; use $_SERVER['DOCUMENT_ROOT']
As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.
The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html.
Using correct syntax and the above $_SERVER value (which will point to the /public_html folder you will have:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
This will work from any script within your directory structure, if the file (PHPMailerAutoload.php) exists and is reachable at that given location
Given your location
/public_html/classes/filethatwantstoacces.php
doing ../ gives you
/public_html/classes
so ../PHPMailer/PHPMailerAutoload.php evaluates to
/public_html/classes/PHPMailer/PHPMailerAutoload.php
As #Martin has pointed out, using $_SERVER['DOCUMENT_ROOT'] to construct an absolute path to your file is the easiest way to avoid relative directory navigation errors such as this:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.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);

Include statement in php file

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.

PHP Require Path

I have a directory layout something like /public_html/foo/bar.php that I want required in my file with my file require statement being currently require('../foo/bar.php') but I'm not getting to it (I keep getting an error saying its not a directory? and failed to open stream).
My file that I'm putting the require in is in /public_html/foo2/ so I need to go up one directory listing and then find the file to include it but I'm not sure what I'm doing wrong right now. Any guesses?
Actual code:
require('../objects/user.php/');
Thanks for your help!
You have a / behind your ../objects/user.php suggesting user.php is a directory.
That's why it tells you it is not a directory, because it indeed isn't.
Remove it and you'll probably be fine. (so ../objects/user.php/ becomes ../objects/user.php)

Categories