require_once in sub-folder issue - 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.

Related

$_SERVER['DOCUMENT_ROOT'] does not include PHP project Name [duplicate]

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();

Folder Path issues when requiring files

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';

How to include config file in multiple directories php pages

I have a following directory structure for my project in which I have Init.php file under core folder. I wanted to include this file on every pages under the views folder to auto load all my classes defined under classes folder. But when I am including require_once '../../core/Init.php'; from a sub-directory of views folder it gives me following error
require_once(classes/Config.php): failed to open stream: No such file or directory in
Including this file in every pages under the views folder
require_once 'core/Init.php';
core/Init.php
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
My directory structure is as below
I would like to include this single file (core/Init.php) into all my files, directories and sub-directories of views folder. Any one have an idea how would I do it.
Thanks!
The root of your problem is that your code actually gets executed from multiple directories.
Instead it should be called through a single point of entry (usually referred to as "bootstrap file").
Basically, you do something like this.
$config = require __DIR__ '/../config.php';
$page = $_GET['page'] ?? 'home';
if (in_array($page, $whitelist)) {
require __DIR__ . "/path/to/pages/{$page}.php";
}
Oh, and don't refer to your template as "views". They are not.
Structure! When starting the application tell it where to find things then add the spl_autoload_register function and setup the things which are always accessible:
.../www/index.php
eg (in index.php):
chdir(__DIR__):
Now every thing starts at .../www/ directory. From that point you can start to setup all paths you wish to include. Relative or absolute.
Kind regards

rurequire_once file not found if called from within a folder

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';

include files from anywhere on the server

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();

Categories