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.
Related
I have my all files in public_html directory like this
global_assets
assets/layouts/header.php
assets/setup/env.php
order/index.php
order/info/index.php
Now all is working fine in order/index.php in which I am including header like
include '../assets/layouts/header.php';
Now I have tried include header in my order/info/index.php like below
include '../../assets/layouts/header.php';
Header getting included properly in this too, but its giving me error called
PHP Fatal error: require(): Failed opening required '../assets/setup/env.php' in /home/myusername/public_html/assets/layouts/header.php on line 7
I will have many similar errors because of directory structures. I think I am not following proper way to include files so I can call it properly from any directory, sub directories without any issues. Let me know if anyone here can help me for it.
Thanks a lot!
You can define and retrieve the root of your project in several ways, then you can always access php files from this root.
environment variable
include getenv('MY_PROJ_ROOT').'/assets/...';
auto-prepend-file
//php.ini
auto_prepend_file="/path/to/prepend-file.php"
//prepend-file.php
define('MY_PROJ_ROOT', '/path/to/project-root');
//any other files
include MY_PROJ_ROOT.'/assets/...';
include-path
//php.ini
include-path=".:/path/to/project-root"
//other files
include 'assets/...';
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.
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';
I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.
I get the following error in one of my function includes.
require(../htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory
I can correct this problem if I code the path
./htmlpurifier/library/HTMLPurifier.auto.php
But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function include every time which defeats the purpose of my include. Is there a way I can have this work with out having to re-code the path every time? Is there a way to include the full path?
Try this:
require realpath(dirname(__FILE__) . '/htmlpurifier/library/HTMLPurifier.auto.php');
That way you always include files relative to the path of the current file.
You are using wrong path.
Just put these 2 lines together and feel the difference.
../htmlpurifier/library/HTMLPurifier.auto.php
./htmlpurifier/library/HTMLPurifier.auto.php
That's 2 different paths and will never work at the same time. Latter one points to the current directory while first one - one level higher.
But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function
that's sensible desire. Here you go:
require $_SERVER['DOCUMENT_ROOT'].'/htmlpurifier/library/HTMLPurifier.auto.php';
(assuming this htmlpurifier folder lays in the document root)
Try this
require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');
How about configuring the path to this .php file as a configuration variable in your script? Presumably you'll be loading some common functions library in all your pages, so just do:
$PATH_TO_PURIFIER = 'C:\this\that\whatever\HTMLPurifier.auto.php';
in that common file, then you can just do
include('commonstuff.php');
include($PATH_TO_PURIFIER);
As long as commonstuff.php is somewhere in your PHP's include_path, it's far more reliable to do it this way rather than try to dynamically calculate the path everywhere else. Especially if your directory structure might change, rendering the calculated path invalid and forcing you to change every PHP file yet again.