So, I followed a PHP OOP Tutorial on YouTube which was good, got me the code i needed, but now I'm trying to implement it into my site, and i'm having a bit of an error problem.
Here's my folder structure so you can see whats going on. The root folder 'modelawiki' is within my 'htdocs' folder within a XAMPP Localhost Server. I have other sites in other folders within my 'htdocs' folder. http://imgur.com/a/vs0qt
In my index.php file within my root folder (modelawiki), I'm requiring my 'core/init.php' file using the following code:
require_once 'core/init.php';
Which executes just fine. But when I move into my 'admin' folder and try to execute:
require_once '../core/init.php';
I'm coming up with the following error:
Warning: require_once(functions/sanitize.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/modelawiki/core/init.php on line 31
Fatal error: require_once(): Failed opening required 'functions/sanitize.php' (include_path='.:/Applications/XAMPP/xamppfiles/lib/php') in /Applications/XAMPP/xamppfiles/htdocs/modelawiki/core/init.php on line 31
Heres my 'core/init.php' code:
// Auto Load Classes
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
// Load Functions
require_once 'functions/sanitize.php';
How would I fix this issue in my 'core/init.php' file to be able to load from within the root folder, and any deeper folders within my folder tree? I also need to make sure that once I upload this to my Network Solutions FTP server that it will run as well.
You could use absolute paths instead:
get directory of root in your classloader
// if called in core/init.php
// the following will be the absolute path of whatever folder core is in
$rootdir = dirname(dirname(__FILE__));
so from there you'll have something like this:
$rootdir = dirname(dirname(__FILE__));
// Auto Load Classes
spl_autoload_register(function($class) {
require_once $rootdir . 'classes/' . $class . '.php';
});
// Load Functions
require_once $rootdir . 'functions/sanitize.php';
I think you should change init.php file as below. You have to give the path correctly.
require_once '../functions/sanitize.php';
Related
I have website running perfectly on production server. I have moved it to another web server. (VPS).
Let me explain you with example:
The directory structure:
includes/
header.php
business/
index.php
some other files...
index2.php
In my previous version, I used
include_once(includes/header.php)
in index.php and index2.php. It runs fine. but in my new server setup it's giving me error regarding path (obvious).
ERROR:
include_once(includes/header.php): failed to open stream: No such file or directory
And because of that:
Fatal error: Class 'EncryptionClass' not found
I think there are some server configurations which I need to do. But, I don't know how?
Please guide me. Let me know if you want more information.
if using PHP 5.3+ Try using:
include_once(__DIR__.'/includes/header.php');
DIR is a magic constant that will return the full directory that the file is in.
You could supply an absolute file system path to the include:
include_once($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");
I would simply add your includes directory to the include_path. For example, in index2.php
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/includes', // relative to this file, index2.php
get_include_path()
]));
include_once 'header.php';
and similarly in business/index.php...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/../includes', // relative to this file, business/index.php
get_include_path()
]));
include_once 'header.php';
Personally, I would use PSR-0 file-to-class name mappings and configure an autoloader, eg
includes/EncryptionClass.php
class EncryptionClass { ... }
index2.php
spl_autoload_register(function($class) {
require_once __DIR__ . '/includes/' . $class . '.php';
});
$encryptionClass = new EncryptionClass();
I've read nearly all of the posts about set_include_path but I haven't found a post that I really understand yet.
I'm trying to use UserSpice for my logins on a new site. I already have a file structure for the site of course, and I thought I would be able to simply add the UserSpice folder to that structure and then use set_include_path to access all of the files within UserSpice. But I guess I just don't understand the function or paths in general.
This is my code:
set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\xampp\htdocs\mynewsite\userSpice');
This is the result:
Fatal error: require_once(): Failed opening required 'users/init.php' (include_path='.;C:\xampp\php\PEAR;C:\xampp\htdocs\mynewsite\userSpice') in E:\xampp\htdocs\mynewsite\index.php on line 10
The 'users' folder is a subfolder of 'mynewsite/userSpice' so I thought that the path would allow access to that folder.
Can anyone help?
You have to use $_SERVER['DOCUMENT_ROOT'] to access to the required path:
require_once $_SERVER['DOCUMENT_ROOT'] .'/mynewsite/userSpice/users/init.php';
require_once $_SERVER['DOCUMENT_ROOT'] .'/mynewsite/userSpice/users/includes/header.php';
require_once $_SERVER['DOCUMENT_ROOT'] .'/mynewsite/userSpice/users/includes/navigation.php';
I am using a php framework (not a common one) and I am running into an issue. Whenever I add a sub-folder, my framework cannot be found. For instance, here is what my folder structure looks like.
+ = file
-account
-classes
-core
+index
+register
I have an init file within my core folder that works perfectly fine with files within the root, but when I am in files with the account folder (files for when logged in) it throws errors.
I have the following in my init file to call the classes.
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
So normally I do this to call the init file:
require_once 'core/init.php';
I have tried doing this
require_once '/core/init.php';
I get this error:
Warning: require_once(/core/init.php): failed to open stream: No such file or directory
Then when I do this
require_once 'http://sitename.com/core/init.php';
The init file works, but then the path to all of my classes does not work.
Do I need to do something with my init file, specifically these lines:
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
To allow it to work for root files and sub-folder files?
If I understood correctly you are trying to require the core/init.php file from withing an file inside the classes directory? If so try using
require_once __DIR__ . '/../core/init.php';
The magic constant __DIR__ will refer to the current directory and the ../ will refer to its parent directory where the core directory is located.
I have a website built through PHP using PDO. In my init.php file I use the spl_autoload_register method and then I require_once a sanitize.php file to sanitize data. When I require the core/init.php file from the root folder, it all works fine, but if I make a folder inside of root, no matter what it is called, and I require ../core/init.php then it throws this error:
[03-Apr-2014 22:10:06 Europe/Berlin] PHP Warning: require_once(functions/sanitize.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/project1/login/core/init.php on line 29
This is the init.php file:
// Autoload classes
function autoload($class) {
require_once 'classes/' . $class . '.php';
}
spl_autoload_register('autoload');
// Include functions
require_once 'functions/sanitize.php';
Any ideas?
UPDATE
File Structure:
root
core
init.php
classes
... all classes
css
functions
sanitize.php
processes
login.inc.php
js
script.js
index.php
Inside the script file, I am calling the login.inc.php file. If the login.inc.php file is outside of the processes folder, it works fine and there are no errors, if it is inside the processes folder, it throws that error.
Use the docroot variable:
$doc_root = $_SERVER['DOCUMENT_ROOT'];
require_once($doc_root . "/functions/sanitize.php");
require_once __DIR__ . '/functions/sanitize.php';
I have website running perfectly on production server. I have moved it to another web server. (VPS).
Let me explain you with example:
The directory structure:
includes/
header.php
business/
index.php
some other files...
index2.php
In my previous version, I used
include_once(includes/header.php)
in index.php and index2.php. It runs fine. but in my new server setup it's giving me error regarding path (obvious).
ERROR:
include_once(includes/header.php): failed to open stream: No such file or directory
And because of that:
Fatal error: Class 'EncryptionClass' not found
I think there are some server configurations which I need to do. But, I don't know how?
Please guide me. Let me know if you want more information.
if using PHP 5.3+ Try using:
include_once(__DIR__.'/includes/header.php');
DIR is a magic constant that will return the full directory that the file is in.
You could supply an absolute file system path to the include:
include_once($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");
I would simply add your includes directory to the include_path. For example, in index2.php
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/includes', // relative to this file, index2.php
get_include_path()
]));
include_once 'header.php';
and similarly in business/index.php...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/../includes', // relative to this file, business/index.php
get_include_path()
]));
include_once 'header.php';
Personally, I would use PSR-0 file-to-class name mappings and configure an autoloader, eg
includes/EncryptionClass.php
class EncryptionClass { ... }
index2.php
spl_autoload_register(function($class) {
require_once __DIR__ . '/includes/' . $class . '.php';
});
$encryptionClass = new EncryptionClass();