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;
});
?>
Related
In an application I am calling require_once $file where I have validated that the $file variable represents an absolute path, the file exists, and is readable. PHP 7.3 is giving me the dreaded: "Fatal error: require_once(): Failed opening required" message.
When I change the code to use "require" instead of "require_once" I get further, till the next "require_once" statement.
I am running PHP 7.2.19 on ubuntu 18.04.
This particular code is embedded deep within an application that has already included and required many different files at different times.
I have checked for obvious things like the particular file being included multiple times (it is not)... and including a different file for testing purposes, and that also consistently fails. I have double checked permissions, etc.
$file = "$destdir/lib/include.php";
$file = realpath($file);
if( is_file($file) && is_readable($file) ) {
require_once $file; // fails
// require $file; // works, but fails when require_once is called within this file.
// include $file; // works, but fails when require_once is called within this file
// include_once $file; // fails
}
I expected of course, that the file is required.
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');
I want to move an excel file from one folder to another, I have used the following line of code
$approve_employee_id = $this->input->post('approve_employee_id');
$approve_month_id = $this->input->post('approve_month_name');
$approve_year_name = $this->input->post('approve_year_name');
$approve_employee_username = $this->input->post('approve_employee_username');
$folder_name = $approve_month_id.$approve_year_name;
rename(".files/".$folder_name."/un_approved/".$approve_employee_username, ".files/".$folder_name."/approved/".$approve_employee_username);
I keep on getting the following error from the system :
A PHP Error was encountered
Severity: Warning
Message: rename(.files/January2013/un_approved/HS0103,.files/January2013/approved/HS0103): The system cannot find the path specified. (code: 3)
Filename: controllers/time_sheet.php
Line Number: 279
My folder tree is as follows :
C:\xampp\htdocs\timesheet\files\January2013\approved for approved and C:\xampp\htdocs\timesheet\files\January2013\un_approved for un approved
Please advise on the best way to implement this ?
Perhaps you should use "./files", instead of ".files" ...?
However, I'd suggest to decouple the location of your data files from the location of your php script... So, I should do:
...
$basedir = $_SERVER['DOCUMENT_ROOT'] . "/timesheet/files/$folder_name";
rename("$basedir/un_approved/$approve_employee_username", "$basedir/approved/$approve_employee_username);
This approach makes your life easier when you should deploy your code do different web servers, for example...
use __DIR__.'./../yourfolder/'
I tried and it works
I just stick to full path as it makes it much more easy to understand ...
for example
dirname(__FILE__).'/files/etc/etc/path.yourfile';
you can go up the tree by
dirname(dirname(__FILE__)).'/files/etc/etc/path.yourfile'; and so on
You can try this ...
$files_folder = dirname(__FILE__); //or go up as required.
Then
rename($files_folder."/".$folder_name."/un_approved/".$approve_employee_username, $files_folder."/".$folder_name."/approved/".$approve_employee_username);
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
i have to fetch data from web page using phpquery and i have already used a example
this following is the code...
<?php
require_once(dirname(__FILE__) . "/phpQuery.php");
phpQuery::browserGet('http://www.google.com/', 'success1');
function success1($browser) {
$browser
->WebBrowser('success2')
->find('input[name=q]')
->val('search phrase')
->parents('form')
->submit();
}
function success2($browser) {
print $browser;
}
and right now i have a new error that is
Warning: require_once(WebBrowser.php): failed to open stream: No such file or directory in /var/www/TantraProjects/phpQuery/phpQuery.php on line 4922 Fatal error: require_once(): Failed opening required 'WebBrowser.php' (include_path='.:/usr/share/php:/usr/share/pear:/var/www/TantraProjects/phpQuery/phpQuery/:/var/www/TantraProjects/phpQuery/phpQuery/plugins/') in /var/www/TantraProjects/phpQuery/phpQuery.php on line 4922
i cant understand why this is shown , help me...
most obviously you don't have phpQuery.php in the same directory as you script - mind you: On a Linux webhost filenames are case-sensitive.
It's an additional file that needs to be available for reading.
The phpQuery WebBroswer is a plugin. Check that you have put that file (WebBrowser.php) into the location it gives you the error and that it's available to be read, e.g.
/var/www/TantraProjects/phpQuery/WebBroswer.php