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';
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();
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';
I tried to use the absolute path to include my files :
I have 4 files (I have other file on my localhost but oddly the inclusion works well) :
header.php (C:\wamp\www\MySite\layout\header.php)
<?php session_start ();
require_once '/pdo.php';
....
pdo.php (C:\wamp\www\MySite\pdo.php)
<?php
require_once '/class/User.php';
require_once '/class/Order.php';
....
forms/login.php (C:\wamp\www\MySite\forms\login.php)
<?php
session_start ();
include '/pdo.php';
....
login.php (C:\wamp\www\MySite\login.php)
<?php
$title = 'Connexion';
include ("/layout/header.php");
...
So it's look like :
Root
- forms
- login.php
-layout
- header.php
- pdo.php
- login.php
And I have this errors :
( ! ) Warning: include(/pdo.php): failed to open stream: No such file or directory in C:\wamp\www\MySite\forms\login.php on line 3
Call Stack
( ! ) Warning: include(): Failed opening '/pdo.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\MySite\forms\login.php on line 3
( ! ) Fatal error: Class 'User' not found in C:\wamp\www\MySite\forms\login.php on line 12
But I have this problem on a lot of files since I wanted to change the arboresence (tree) of files and folder ..
How I can solve this problem ? and how I can do to avoid this problem in the future ?
Thank you
I tried to use the absolute path to include my files :
header.php (C:\wamp\www\MySite\layout\header.php)
<?php session_start ();
require_once '/pdo.php';
The PHP code is executed on the server. An absolute path in this context means a file-system absolute path, not a web host path. Since you are on Windows, /pdo.php in fact means C:/pdo.php and not C:\wamp\www\MySite\pdo.php as it seems you think.
The best way to work with paths in PHP, regarding include and require is to use the __FILE__ and __DIR__ magic constants and the dirname() PHP function to build the (file-system) absolute paths of files starting from their relative locations.
Your code becomes:
header.php (C:\wamp\www\MySite\layout\header.php):
<?php
session_start ();
// 'pdo.php' is one level up
require_once dirname(__DIR__).'/pdo.php';
....
pdo.php (C:\wamp\www\MySite\pdo.php)
<?php
// User.php is inside the 'class' subdirectory
require_once __DIR__.'/class/User.php';
require_once __DIR__.'/class/Order.php';
....
dir1/dir2/dir3/file.php (C:\wamp\www\MySite\dir1\dir2\dir3\file.php)
<?php
// 'header.php' is in the 'layout' subdirectory of the grand-grand parent directory
include dirname(dirname(dirname(__DIR__))).'/layout/header.php';
Remark
The solution presented here makes the code independent of its actual location in the file system. You can move the entire project (everything in C:\wamp\www\MySite) in a different directory or on a different computer and it will work without changes. Even more, if you use forward slashes (/) as directory names separators it works on Windows, macOS or any Linux flavor.
One convention is to include a configuration file in every php script. This configuration file would set the include path, allowing you to include other files, classes, etc and would continue to work regardless of whether your current working directory had changed - it will allow you to better organise your classes and functions into meaningful directories and include them without worrying about the full path:
An example below:
Create file at C:\wamp\www\MySite\config.php
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\wamp\www\MySite\class' . PATH_SEPARATOR . 'C:\wamp\www\MySite\conf');
?>
Then in
header.php (C:\wamp\www\MySite\layout\header.php)
<?php
require_once('C:\wamp\www\MySite\config.php');
session_start ();
require_once '/pdo.php'; // put pdo.php in C:\wamp\www\MySite\conf\ directory and it will be included
....
In pdo.php (C:\wamp\www\MySite\pdo.php):
<?php
require_once 'User.php';
require_once 'Order.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 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();