I have some big problems with path to my php files. I got a suggestion that I should define my path for the root. But is this define correct, or do I need something else on it?
<?php
// define path to application root
define( 'APP_ROOT', dirname(__FILE__) );
require_once( APP_ROOT . '/resources/includes/header.html' );
?>
Because I still get the same error:
Warning: require(/Applications/MAMP/htdocs/website/resources/auth/resources/includes/header.html): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/website/resources/auth/profile.php on line 4
Fatal error: require(): Failed opening required '/Applications/MAMP/htdocs/website/resources/auth/resources/includes/header.html' (include_path='.:/Applications/MAMP/bin/php/php5.6.2/lib/php') in /Applications/MAMP/htdocs/website/resources/auth/profile.php on line 4
Updated:
Related
I'm having problems debugging this php error. '/app/bootstrap.php' exists so not sure why I'm getting this error? Can anyone help? Thanks
[24-Mar-2017 10:42:50 Europe/London] PHP Warning: require_once(/app/bootstrap.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/example.com/index.php on line 2
[24-Mar-2017 10:42:50 Europe/London] PHP Fatal error: require_once(): Failed opening required '/app/bootstrap.php' (include_path='.:/Applications/MAMP/bin/php/php5.6.30/lib/php') in /Applications/MAMP/htdocs/example.com/index.php on line 2
The path /app/bootstrap.php looks like a root path ( and the root isn't your website root, but your Mac root directory).
Change require path to __DIR__ . '/app/bootstrap.php', it means to require a sccript that in /app/bootstrap.php relative to CURRENT directory.
Here is the structure of my application :
core
application
controller_base.class.php
registry.class.php
router.class.php
template.class.php
controller
include
config.php
model
views
index.php
The file config.php is included in the index.php file (there's no problem with this) and here is what it contains :
<?php
/*** include the controller class ***/
include(ROOT . '/core/application/controller_base.class.php');
/*** include the registry class ***/
include(ROOT . '/core/application/registry.class.php');
/*** include the router class ***/
include(ROOT . '/core/application/router.class.php');
/*** include the template class ***/
include(ROOT . '/core/application/template.class.php');
/*** autoload model classes ***/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = ROOT . '/core/model/' . $filename;
if (!file_exists($file)) {
return false;
}
// include the $class_name class
include($file);
}
$registry = new registry;
You have to know that the constant ROOT is defined like this :
define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
This constant contains the value /Lab/Livre/POO/TP1 which is of course the root of the applications.
But there's a problem whith all the files I include in config.php. PHP throws me an error that says the files are not found in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php.
So the problem is clear. I want PHP to look for the files from the root of the application (where index.php is), but it looks for it from the folder where config.php is located.
I had this problem for a long time but I want to find a solution once and for all.
I can't get it to do want I want ! I hope someone will help me.
Thank you very much.
P.S.: In other words, i want to include the files using an absolute path.
P.S.: Here is the full error text :
Warning: include(/Lab/Livre/POO/TP1/core/application/controller_base.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/controller_base.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(/Lab/Livre/POO/TP1/core/application/registry.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/registry.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(/Lab/Livre/POO/TP1/core/application/router.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/router.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(/Lab/Livre/POO/TP1/core/application/template.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/template.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
I have found a solution to my problem. I set the ROOT constant to this :
define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
But what went wrong ?
Getting this warning when trying to run my local PHP project (index.php):
Warning: require_once(/Controller/BulletinController.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/WEBB_SERVER/6.2.1x/index.php on line 2
Fatal error: require_once(): Failed opening required '/Controller/BulletinController.php' (include_path='.:') in /Library/WebServer/Documents/WEBB_SERVER/6.2.1x/index.php on line 2
No such file or directory? This is my first 6 rows in index.php:
<?php
require_once ("/Controller/BulletinController.php");
require_once ("/General/MasterPage.php");
require_once ("./General/dbConnection.php");
session_start();
I've tried giving BulletinController.php 777 rights (just developing locally), but no luck.
This is what my structure looks like, so everything is in place:
Thankful for any advice you may have.
require_once ("/Controller/BulletinController.php");
must be
require_once ("Controller/BulletinController.php");
I think you need the whole path:
require_once dirname(__FILE__) . "/Controller/BulletinController.php";
Hello i'm running WAMP (32 Bit) server
i exec C:\wamp\www\1\index.php using browser with this address http://localhost:8080/1/
index.php has the follow code
require_once 'libraries/database/database.php';
database.php has the follow code
require_once '../misc/traits/singleton.php';
singleton.php is located in this location
C:\wamp\www\1\libraries\misc\traits\singleton.php
database.php is located in this location
C:\wamp\www\1\libraries\database\database.php
and there is error(s):
( ! ) Warning: require_once(../misc/traits/singleton.php): failed to open stream: No such file or directory in C:\wamp\www\1\libraries\database\database.php on line 3
( ! ) Fatal error: require_once(): Failed opening required '../misc/traits/singleton.php' (include_path='.;C:\php\pear') in C:\wamp\www\1\libraries\database\database.php on line 3
Thanks
Paths stem from the php file that was originally executed, unless you change the path using chdir.
So use 'libraries/misc/traits/singleton.php'
If you want to do relative includes, use __DIR__, which is the directory of the CURRENT file.
e.g.
__DIR__ . '/../misc/traits/singleton.php'
I'm new to PHP and I need to use some classes from an external library in my project.
My .php file, SMMain.php is in /Library/WebServer/Documents directory on my Mac. I placed the classes from the outside library right there next to it. All of these files are from the namespace Parse;. Some of the classes are in a directory called Internal.
No matter what require_once I try I get errors that the files are not found. Here's what I tried:
require_once('\Internal\Encodable.php');
produces this error:
Fatal error: require_once(): Failed opening required '\Internal\Encodable.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 13
Next try:
require_once('/Internal/Encodable.php');
produces this:
Fatal error: require_once(): Failed opening required '.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 7
I also tried:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) .'/Internal/Encodable.php');
Gives no error for the /Internal/Encodable file, but this produces another error:
Fatal error: Class 'ParseObject' not found in /Library/WebServer/Documents/SMMain.php on line 24
ParseObject.php is in the root directory together with my SMMain.php. So I tried requiring it like this:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) . '/ParseObject.php');
which gives this error:
Fatal error: Class 'ParseObject' not found in /Library/WebServer/Documents/SMMain.php on line 24
and if I try this:
require_once(realpath($_SERVER["DOCUMENT_ROOT"]) . 'ParseObject.php');
the error is:
Fatal error: require_once(): Failed opening required '/Library/WebServer/DocumentsParseObject.php' (include_path='.:') in /Library/WebServer/Documents/SMMain.php on line 17
So what's the solution?
Use require_once($_SERVER["DOCUMENT_ROOT"] .'/Internal/Encodable.php'); if the Internal directory is under the server document root and the Encodable.php is in there.
You can use the echo getcwd(); to see, where is the current working directory.
I am always doing that, when i call my index.php file (it handles all other files), to determinate, where is my base path, and store it in a constant. So lately, i will always include files with that path as an absolute path.