I haven't seen any error in my files but when I run my code it shows me the following error:
Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7
Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7
My code is:
classes/Core.php
<?php
class Core {
public function run() {
ob_start();
require_once(Url::getPage());
ob_get_flush();
}
}
inc/autoload.php
<?php
require_once('config.php');
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
index.php
<?php
require_once('inc/autoload.php');
$core = new Core();
$core->run();
I know it's a bit late. I was researching on this because I had the same problem. This is what i tried and it worked for me.
// this code is for the inc/autoload.php file.
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(dirname(__FILE__) . "/config.php");
//require_once(inc/config.php);
function __autoload($className){
$class = explode("_",$className);
$path = implode("/",$class).".php";
require_once($path);
}
Please I just started learning PHP and i stand corrected by you guys.
I hope this helps.
Your Core class is apparently defined at:
C:\xampp\htdocs\completed\classes\Core.php
But you attempt to load this file:
C:\xampp\htdocs\completed\Core.php
It isn't a good idea to build a class auto-loader that works with relative paths*. I suggest you add a prefix here to build an absolute path:
$path = implode("/", $class).".php";
E.g.:
$path = __DIR__ . '/../classes/' . implode("/", $class).".php";
(*) Among other reasons, because relative paths in PHP are relative to the main script (rather than the file when path usage happens) so the source directory depends on what script you load autoload.php from.
I've faced the same problem with yours. I think it's late for you. But may be it's helpful for others.
I didn't included the config.php file in autoload.php file
inc/autoload.php
//require_once('config.php');
I included config.php file in the index.php like this:
index.php
require_once('inc/config.php');
require_once('inc/autoload.php');
And it's worked fore me, I hope this for you, too :). Thanks for your time to read my comment.
because in same folder
require_once('inc/autoload.php');
require_once('config.php');
so.. i think may be should like this
require_once('inc/config.php');
Related
Good afternoon to everyone.
For the past 3 days, I've been struggling with PHP's Autoloading features, which aims to simplify and unify a program's class files, all located in one executing file. For the context, I am currently learning the OOP perspective of PHP from this tutorial videos to not only further understand PHP's OOP understanding before doing a PHP OOP project from college, but also to try to create my own PHP program, so do ELI5 about the solution to me, please.
From what I've learned from the following videos: Video 1 and Video 2, the autoload codes I written from what I've learned appears to work fine, however, it gave two warnings:
Warning: include(/classes/person.php): failed to open stream: No such file or directory in C:\xampp\htdocs\CollegeTimetableApp\includes\autoloader.php on line 12
Warning: include(): Failed opening '/classes/person.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\CollegeTimetableApp\includes\autoloader.php on line 12
And, it gave a fatal error message as follows:
Fatal error: Uncaught Error: Class 'person' not found in C:\xampp\htdocs\CollegeTimetableApp\includes\index.php:33 Stack trace: #0 {main} thrown in C:\xampp\htdocs\CollegeTimetableApp\includes\index.php on line 33
I will include downloadable files so that anyone check and verify it with me.
The Autoloader codes on the autoloading file:
<?php
spl_autoload_register(function($className) {
include str_replace("\\","/","\classes").'/'.str_replace('\\',"/",$className).'.php';
});
?>
Simple executing codes on the index file:
<?php
$person1 = new person();
$person1->SetName("Kai");
echo $person1->name;
?>
My questions: what's wrong, how to create a functioning autoload feature, and are there any additional corrections that can be made from my program?
Thank you.
Try including $_SERVER['DOCUMENT_ROOT'] to your path, as well as the sub folder in that folderwhich contains your app.
<?php
spl_autoload_register(function($className) {
$file = $_SERVER['DOCUMENT_ROOT']; // This is the folder you serve files from
$file .= '/CollegeTimetableApp' // This is the folder in which your app lives
$file .= str_replace("\\", "/", "\classes");
$file .= '/'; // Consider using DIRECTORY_SEPARATOR instead
$file .= str_replace('\\', "/", $className);
$file .= '.php';
include $file;
});
?>
I am getting the following error:
Warning: require_once(C:\xampp\htdocs\mvc\public\views\home\init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mvc\public\index.php on line 3
Fatal error: require_once(): Failed opening required 'C:\xampp\htdocs\mvc\public\views\home\init.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\mvc\public\index.php on line 3
In Index.php, my code is
<?php
require_once __DIR__ . '\views\home\init.php';
$app= new app;
?>
In init.php, my code is
<?php require_once __DIR__ . '\core\app.php';
require_once __DIR__ . '\core\controller.php';
In controller.php:
<?php
class controller
{
}
?>
In app.php
<?php
class app
{
public function _construct()
{
echo 'now what';
}
}
?>
My folder paths are:
htdocs\mvc\app\views\home\init.php
htdocs\mvc\app\core\app.php & controller.php
htdocs\mvc\public\index.php
Anybody please help? i have tried require_once (_DIR . ""); as well but not working.
Try this:
First do echo for the file path in index.php file like -
echo __DIR__ . '/views/home/init.php';
And then check whether the path is correct or not. If not, then change it accordingly and then use the path in require_once.
You can also test the permission of directory and file itself. There could be an issue of executing file if permission is read only.
restore the index.php file in your public folder.... the error will clear out.
Put index.php file in your public folder.... the error will clear out.
tested and verified
I am getting the following error from a Windows server:
Warning: require_once(../../conf/config.php): failed to open stream: No such file or directory in \WDP\DFS\30\6\3\2\3039591236\user\sites\5213291.site\www\webmap\inc\class\conf.php on line 2
Fatal error: require_once(): Failed opening required '../../conf/config.php' (include_path='.;C:\php\pear') in \WDP\DFS\30\6\3\2\3039591236\user\sites\5213291.site\www\webmap\inc\class\conf.php on line 2*******
This is a simple query to MYSQL table on a Windows server. I have the files in www/webmap. Here is the relevant part of the config.php
<?php
require_once(dirname(dirname(__FILE__)) . '/inc/class/csite.php');
require_once(dirname(dirname(__FILE__)) . '/inc/class/users.php');
require_once(dirname(dirname(__FILE__)) . '/inc/class/dbc.php');
require_once(dirname(dirname(__FILE__)) . '/inc/class/resize.php');
require_once(dirname(dirname(__FILE__)) . '/inc/class/func.php');
My understanding is that the config.php is found as I included:
echo "Does file exist?". file_exists($file) ? 'true' : 'false';
echo "Is it readable?".is_readable($file) ? 'true' : 'false'; require_once $file;
which returns "true, true"
The error in your program is located in this file.
WDP\DFS\30\6\3\2\3039591236\user\sites\5213291.site\www\webmap\inc\class\conf.php
On line 2...
require_once '../../conf/config.php';
Based on this error message, the file your program is looking for is this...
WDP\DFS\30\6\3\2\3039591236\user\sites\5213291.site\www\webmap\conf\config.php
To fix your program, you need to make sure that your require_once function is properly pointing to the config.php file you're looking for.
File Path Examples
\www\conf\config.php
require_once '../../../conf/config.php';
\www\webmap\conf\config.php
require_once '../../conf/config.php';
\www\webmap\inc\conf\config.php
require_once '../conf/config.php';
\www\webmap\inc\class\conf\config.php
require_once 'conf/config.php';
If things are getting rough using ../'s, try finding your file from the root by beginning your path with a forward slash.
require_once '/webmap/inc/conf/config.php';
You can also try this to make sure you are reading from the proper relative directory:
require_once dirname(__FILE__) . '/../../conf/config.php';
My final offer:
Based on the file location's you've supplied in the comments, I can only recommend that you try one of these sets.
require_once '../../conf/config.php';
require_once '../conn.php';
require_once '/webmap/conf/config.php';
require_once '/webmap/inc/conn.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/webmap/conf/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/webmap/inc/conn.php';
I am vague at understanding the include paths and how they are written out, I know how to set them within the ini file and how to functionally do it set_include_path just not how to get it to be exact each time no matter what.
So I have an admin autoload file that I include in all my headers to register the spl_autoload_register function. I just keep getting errors in my error_log file. It says that
PHP Fatal error: Class 'Configurate' not found in
/home/~username~/public_html/testing_ini.php on line 5
So what I am looking for is how can I set the include path to always be the directory before the public_html directory no matter where I am?
I've tried setting the include path to such
.:/opt/alt/php5/usr/share/pear:/opt/alt/php5/usr/share/phphome/~username~/classes/Configurate.php
But I still get the error. Any help and some tips to understanding this entire thing? I suck at relative paths
As per requested spl_autoload_register function
<?php
$ini = parse_ini_file("configurations.ini",true);
foreach($ini as $section) {
foreach($section as $key=>$value ) {
define("__".strtoupper($key)."__",$value);
}
}
//if(__USERNAME__ == null) {
// header("Location: /setup.php?step=1");
// exit();
//}
spl_autoload_register(function($class) {
try {
if(!file_exists("../classes/{$class}.php")){
throw new Exception("/classes/{$class}.php does not exist error on line ". __LINE__." in file ". realpath(__FILE__));
} else
require_once "../classes/{$class}.php";
} catch (Exception $ex) {
echo $ex->getMessage()."<br>";
echo $ex->getCode();
}
});
getcwd() and many other methods did not work for me. Though I did find this one method that works great for me in any directory.
set_include_path(
dirname( $_SERVER["DOCUMENT_ROOT"] )
);
Hope this helps anyone else out there looking to get outside the document root and using it like I do.
After reading tons of articles and trying out a lot code I coudn't make it work to include a php file form my localhost. And know I cannot even specify a path in javascript code like
xmlhttp.open("GET","/DB/ajax/ajaxDeleteRow.php?q="+commentId,true);
I would like to include the file: drawTableSqlString.php which is in the same folder as my file: CommentTableAndDeleteAjax.php. they are in the folder: C:\xampp\htdocs\DB\ajax
What I tried so far:
1.
echo get_include_path(); and i got displayed: .;C:\xampp\php\PEAR
then I tried to copy my file into that folder --> didn't work.
echo dirname(drawTableSqlString.php);
I got an error:
Notice: Use of undefined constant drawTableSqlString - assumed 'drawTableSqlString' in C:\xampp\htdocs\DB\ajax\CommentTableAndDeleteAjax.php on line 36
3.
after changing path in php.ini from "include_path=".;C:\xampp\htdocs"" to include_path=".;C:\xampp\php\PEAR"
i tried:
include ('\DB\ajax\drawTableSqlString.php');
include ('DB\ajax\drawTableSqlString.php');
gives an error as aways which looks like this:
Warning: include(\DB\ajax\drawTableSqlString.php): failed to open
stream: No such file or directory in
C:\xampp\htdocs\DB\ajax\CommentTableAndDeleteAjax.php on line 33
Warning: include(): Failed opening '\DB\ajax\drawTableSqlString.php'
for inclusion (include_path='.;C:\xampp\htdocs') in
C:\xampp\htdocs\DB\ajax\CommentTableAndDeleteAjax.php on line 33
Based on other forum entries I also tried all that code below but nothing worked:
//************************** include DB connection INFOS
//$xajax_dir = 'C:\\xampp\\htdocs\\DB\\ajax';
//include( $xajax_dir . DIRECTORY_SEPARATOR . 'drawTableSqlString.php' );
//include('./localhost/DB/ajax/CommentTableAndDeleteAjax.php' );
//include('localhost/DB/ajax/CommentTableAndDeleteAjax.php' );
//include('./DB/ajax/CommentTableAndDeleteAjax.php' );
//include('DB/ajax/CommentTableAndDeleteAjax.php' );
//include('C:/xampp/htdocs/localhost/DB/ajax/drawTableSqlString.php' );
//include_path = ('.;C:\xampp\php\PEAR','C:\xampp\htdocs\DB\ajax\drawTableSqlString.php');
//include('127.0.0.1/DB/ajax/CommentTableAndDeleteAjax.php' );
//include('C:\xampp\htdocs\DB\ajax\drawTableSqlString.php');
//FROM http://stackoverflow.com/questions/17003614/including-files-from-include-path-not-working-as-expected
//include_once ('/DB/ajax/drawTableSqlString');
//include_once("C:/xampp/htdocs/DB/ajax/drawTableSqlString.php");
// include __DIR__ . 'drawTableSqlString.php';
// include '../drawTableSqlString.php';
//include('http://127.0.0.1/DB/ajax/CommentTableAndDeleteAjax.php' );
//$xajax_dir = 'C:\\xampp\\htdocs\\DB\\ajax';
//include( $xajax_dir . DIRECTORY_SEPARATOR . 'drawTableSqlString.php' );
//include ('drawTableSqlString.php');
//include_path=".;c:\php\includes"
//http://www.php.net/manual/en/ini.core.php#ini.include-path
//set_include_path(".;c:\xampp\htdocs\DB\ajax");
//include ('drawTableSqlString.php');
//echo get_include_path();
Any help would be really apreciated.
EDIT:
Since I changed some parts of the php.ini I get an error when staring XAMPP saying:
Fatal Error: Directive 'register_globals' is no longer available in
PHP