set_include_path sets path but path doesn't work - php

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

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

Using absolute paths in PHP not working

I am trying to use absolute paths to get a definitive reference to a config file.
For this, I am currently doing this:
require_once '../config.php'; //Works fine
But when I try this:
require_once '/config.php'; //Throws error, see below
The error I get is:
require_once(): Failed opening required '/config.php' (include_path='.;C:\php\pear')
Is this a thing in PHP 5.6.0 or am I missing something?
Breaking down ../config.php:
../ goes one directory above the working directory
config.php finds the file config.php in that directory
Breaking down /config.php:
/ goes to the server root
config.php finds the file config.php in that directory
If the first one works but not the second, it means that the file config.php is not in the root folder, but is in the parent directory of the working file.
If you explicitly set the includes path in your script to the actual location it should be quite simple to include the files you need such as:
set_include_path( $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR );
require_once( 'config.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();

PHP require_once error with relative path

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:
Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4
So it search the form.php file in include/../include/form.php what is wrong.
I used this code:
require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');
the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php
and the file what I want to test is in: C:\MyProject\include\form.php
What is the problem?
The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.
See the manual entry -> PHP Manual for __DIR__
__DIR__ will return C:\MyProject\phpunit-tesztek\include
You are looking to use the C:\MyProject\include\ path
__DIR__ . '/../../include/Form.php';
I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.
Try removing ../
require_once(DIR.'include/form.php');
Use magic constant DIR :
require_once(__DIR__ . '/../include/form.php');

Categories