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();
Related
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 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 came across a project with this structure.
-projectRoot
--src
---bootstrap.php
--composer.json
--public
---index.php
---css
---images
Now in public directory, index.php contains the following
//in projectRoot/public/index.php
<?php
require __DIR__."/../src/bootstrap.php";
?>
I understand that __DIR__ constant resolve to absolute directory of the current executing php script file.So in the above example I would expect it to be ProjectRoot/public.
the quention
Which path will require in index.php look into ? and what does a parent dir .. in the path above resolve to ?
PS: I read that the whole point of the setup above is to ensure relative paths continue to work regardless where index.php is called. How does that work exactly? thanks.
<?php
require __DIR__ . "/some_file.php";
/* __DIR__ SIMPLY POINTS TO THE DIRECTORY IN WHICH THE ACTIVE SCRIPT LIVES
- THE DIRECTORY OF THE EXECUTING SCRIPT... THEN REQUIRES THE FILE
"some_file.php" WITHIN THAT DIRECTORY.
THE REAL-PATH SHOULD REFLECT SOMETHING LIKE: projectRoot/public/some_file.php
*/
require __DIR__ . "/../src/bootstrap.php";
/* FOLLOWING THE LOGIC ABOVE, __DIR__ . "/../" __DIR__ AGAIN POINTS TO
THE DIRECTORY IN WHICH THE ACTIVE SCRIPT LIVES WHILE "/../" TELLS
THE REQUIRE DIRECTIVE TO LOOK ONE DIRECTORY ABOVE THE CURRENT DIRECTORY
(AND LOCATE WITHIN THAT DIRECTORY ANOTHER DIRECTORY CALLED "src" IN THIS CASE...)
THEN WITHIN THAT "scr" DIRECTORY LOOK FOR A FILE CALLED:
"bootstrap.php" AND REQUIRE/INCLUDE IT...
THE REAL-PATH SHOULD REFLECT SOMETHING LIKE: projectRoot/src/bootstrap.php
*/
// TO UNDERSTAND THESE CONCEPTS MORE CLEARLY, IT WOULD BE ADVISED TO
// TRY SOMETHING LIKE THESE:
var_dump( realpath(__DIR__ . "/../src/bootstrap.php") );
var_dump( realpath(__DIR__ . "/index.php") );
Here's what's happening in your index.php:
__DIR__ // This will be projectRoot/public
/../ // This will go one level up so it'll be projectRoot
src/ // This will go inside the src folder (projectRoot/src)
bootstrap.php // This will open the bootstrap.php
So in the end, it's going to target projectRoot/src/bootstrap.php... that's the file it's requiring.
Not long ago I started using linux as OS principal. But I have a problem to include files.
When I try include a file which in turn includes a third file, I end up getting a error: "failed to open stream: No such file or directory in /var/www/html/..."
For example, my files are distributed in this way:
and code of the 3 files is:
primero.php
require_once '../B/c/segundo.php';
segundo.php
require_once '../d/tercero.php';
tercero.php
echo 'success';
Error:
can someone explain to me what happens? this in windows works. and the truth is I would avoid using "dirname(FILE)"
PD: sorry, I can't post images
Using relative path, sometime is painful. Therefore what I start doing is creating a settings.inc.php file which I keep my important paths. For instance:
try this:
settings.inc.php
<?php
define("ROOT_PATH" , dirname(__FILE__) . "/");
define("CLASS_PATH", ROOT_PATH . "class/");
?>
Then, I require the settings file in the header.php (so I can have it on every page)
require_once("settings.inc.php");
And I use it like
require_once(ROOT_PATH . 'B/c/segundo.php');
And
require_once(ROOT_PATH . 'd/tercero.php');
Using __DIR__ in front of require makes the required path relative to the file in which it is defined. For example:
require_once __DIR__.'/../d/tercero.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();