relative path in require_once doesn't work - php

I have the following structure
otsg
> class
> authentication.php
> database.php
> user.php
> include
> config.inc.php
> encryption.php
> include.php
> session.php
> index.php
> registration.php
include.php file has the following
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';
and in the index.php page I had included
require_once 'include/include.php';
When I open the page index.php I get the following warning and fatal error. I don't understand what causes this error. When I gave the absolute path it works. But absolute path is not a good idea i believe.
Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

Use
__DIR__
to get the current path of the script and this should fix your problem.
So:
require_once(__DIR__.'/../class/user.php');
This will prevent cases where you can run a PHP script from a different folder and therefore the relatives paths will not work.
Edit: slash problem fixed

for php version 5.2.17 __DIR__ will not work it will only works with php 5.3
But for older version of php dirname(__FILE__) perfectly
For example write like this
require_once dirname(__FILE__) . '/db_config.php';

In my case it doesn't work, even with __DIR__ or getcwd() it keeps picking the wrong path, I solved by defining a costant in every file I need with the absolute base path of the project:
if(!defined('THISBASEPATH')){ define('THISBASEPATH', '/mypath/'); }
require_once THISBASEPATH.'cache/crud.php';
/*every other require_once you need*/
I have MAMP with php 5.4.10 and my folder hierarchy is basilar:
q.php
w.php
e.php
r.php
cache/a.php
cache/b.php
setting/a.php
setting/b.php
....

I just came across this same problem, where it was all working fine, up until the point I had an includes within another includes.
require_once '../script/pdocrud.php'; //This worked fine up until I had an includes within another includes, then I got this error:
Fatal error: require_once() [function.require]: Failed opening required '../script/pdocrud.php' (include_path='.:/opt/php52/lib/php')
Solution 1. (undesired hardcoding of my public html folder name, but it works):
require_once $_SERVER["DOCUMENT_ROOT"] . '/orders.simplystyles.com/script/pdocrud.php';
Solution 2. (undesired comment above about DIR only working since php 5.3, but it works):
require_once __DIR__. '/../script/pdocrud.php';
Solution 3. (I can't see any downsides, and it works perfectly in my php 5.3):
require_once dirname(__FILE__). '/../script/pdocrud.php';

Related

Not finding any directory

When using PHP require_once it's not finding the specified directory.
Made sure multiple times this directory does exist!
Also tried this with include. What's the problem here?
File structure:
http://prntscr.com/hut7f0
Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class main
{
public static
$test;
}
require_once '/engine/test.class.php';
main::$test = new Test();
?>
Error:
Warning: require_once(/engine/test.class.php): failed to open stream: No such file or directory in C:\xampp\htdocs\includes\loader.php on line 14
Fatal error: require_once(): Failed opening required '/engine/test.class.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\includes\loader.php on line 14
The problem might be due to usage of an absolute path (path starting with /). PHP then tries to find the file at the root of your filesystem. I guess you want to use a relative path here:
require_once 'engine/test.class.php';
You can use the __FILE__ constant to get the base path of your current file:
require_once dirname(__FILE__) . '/engine/test.class.php';

trouble with require_once failed to open stream: No such file or directory

I'm getting the following error when trying to open the index file of a project that I am taking over on:
Warning: require_once(core_services/logging.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/index.php on line 7
Fatal error: require_once(): Failed opening required 'core_services/logging.php' (include_path='.:/Applications/XAMPP/xamppfiles/lib/php') in /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/index.php on line 7
The code the error refers to looks like this:
7 require_once "core_services/logging.php";
8 $logger=new syslog_class(__FILE__,LOG_DEBUG);
I checked to make sure the filepath exists by defining it and echoing it back as well as the contents of logging.phpin terminal and that returns just fine and when I get the current working directory on line 6, it returns /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com, which I am assuming is defining my root folder.
Not sure exactly how to go about resolving this problem and if anyone has any ideas on approach, I would love to hear them as my PHP is a bit rusty and lacking outside of the context of wordpress.
edit: approaches I've tried so far
require 'core_services/logging.php';
require '/core_services/logging.php';
require './core_services/logging.php';
require '../core_services/logging.php';
require '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';
require realpath(dirname(__FILE__)) .'/core_services/logging.php';
require(__DIR__"/../core_services/logging.php");
require_once 'core_services/logging.php';
require_once '/core_services/logging.php';
require_once './core_services/logging.php';
require_once '../core_services/logging.php';
require_once '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';
require_once realpath(dirname(__FILE__)) .'/core_services/logging.php';
require_once __DIR__"/../core_services/logging.php";
I have also verified that it is not a permissions issue.
Try specifying full path:
require './core_services/logging.php';
//OR
require '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';

Fatal error: require_once(): Failed opening required

i need help on this errer please i intall this script on my easyphp what wrong ?
Warning: require_once(C:/Program Files (x86)/EasyPHP-DevServer-14.1VC9/data/localweb\studentTrans\includes\config.php): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\StudentTranscript Processing System in PHP with MySQL\includes\initialize.php on line 21
hello , i need help on this errer please i intall this script on my easyphp what wrong ?
Fatal error: require_once(): Failed opening required 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC9/data/localweb\studentTrans\includes\config.php' (include_path='.;C:\php\pear') in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\StudentTranscript Processing System in PHP with MySQL\includes\initialize.php on line 21
//define the core paths
/Define them as absolute peths to make sure that require_once works as
expected
//DIRECTORY_SEPARATOR is a PHP Pre-defined constants:
//(\ for windows, / for Unix)
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define ('SITE_ROOT',
$_SERVER['DOCUMENT_ROOT'].DS.'studentTrans');
defined('LIB_PATH') ? null : define ('LIB_PATH',SITE_ROOT.DS.'includes');
// load config file first
require_once "config.php";
//load basic functions next so that everything after can use them
require_once "functions.php";
//later here where we are going to put our class session
require_once "session.php";
require_once "member.php";
require_once "student.php";
require_once "student_details.php";
require_once "student_requirements.php";
require_once "department.php";
require_once "sy.php";
require_once "instructor.php";
require_once "instructorclasses.php";
require_once "studSubjects.php";
require_once "grades.php";
require_once "room.php";
//Load Core objects
require_once"database.php";
//load database-related classes
I don't know the relative position of the file you are trying to include, to the file you execute.
However you can get the path using only DIR and then navigate through it.
For example,
$this_directory = __DIR__;
$file_to_include = $this_directory."\includes\config.php";
require_once $file_to_include;
Hope this helps
you have something wrong in the file "initialize.php" included ..
the problem fixed it's was here !!
defined('SITE_ROOT') ? null : define ('SITE_ROOT', $_SERVER['C:\Program Files (x86)\EasyPHP-DevServer- 14.1VC9\data\localweb'].DS.'studentTrans');

require_once function error when creating webpage in new folder

I am running wamp64 to locally host my website (it is a vbulletin forums used for testing if that helps at all).
I am trying to create a new webpage that is located in a subfolder instead of the root of the website, however I am running into errors.
The webpage I am trying to create is the index.php webpage inside of the addons folder.
My file structure is:
C:
-wamp64
--www (website root)
--global.php
---addons
----index.php
----images
---includes
----config.php
The code I am using for addons - index.php is:
<?php
error_reporting(E_ALL & ~E_NOTICE);
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'addons');
$phrasegroups = array();
$specialtemplates = array();
$globaltemplates = array(
'ADDONSTESTPAGE'
);
$actiontemplates = array();
require_once('/../global.php');
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('ADDONSTESTPAGE') . '");');
?>
When I create a new page in the www folder, it works perfectly (except I use require_once('/global.php'); instead of require_once('/../global.php');.
However, because this is in a subfolder, I am running into the following error due to the files that the global.php is requiring once having their own require_once functions inside of them and those files have their own require_once functions inside of them, etc.
Here is the error I am getting now when I go to localhost/addons/:
Warning: require_once(C:\wamp64\www\addons/includes/init.php): failed
to open stream: No such file or directory in C:\wamp64\www\global.php
on line 20
Fatal error: require_once(): Failed opening required
'C:\wamp64\www\addons/includes/init.php'
(include_path='.;C:\php\pear') in C:\wamp64\www\global.php on line 20
Here are lines 13-20 of global.php:
// identify where we are
define('VB_AREA', 'Forum');
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
// #############################################################################
// Start initialisation
require_once(CWD . '/includes/init.php');
How do I solve this without messing up other files that use the global.php file? I have heard realpath is a common solution for this, but I am new to php and not exactly sure how to use it in this scenario.
All help is appreciated, thank you very much, if you need any more information feel free to ask!
Okay I was able to solve that error by changing require_once(CWD . '/includes/init.php'); to require_once(__DIR__ . '/includes/init.php');.
Now I am getting this error:
Warning: require_once(C:\wamp64\www\addons/includes/class_core.php): failed to open stream: No such file or directory in C:\wamp64\www\includes\init.php on line 51
Fatal error: require_once(): Failed opening required 'C:\wamp64\www\addons/includes/class_core.php' (include_path='.;C:\php\pear') in C:\wamp64\www\includes\init.php on line 51
Class_core.php exists in the includes folder.
Line 51 of init.php is:
require_once(CWD . '/includes/class_core.php');
I tried changing it to require_once(__DIR__ . '/includes/class_core.php'); but it breaks the entire website.
global.php assumes the server process is running from the main C:/wamp64/www directory but because you are running from the addon folder, CWD will be set to C:/wamp64/www/addon , hence all the errors.
To resolve this, do, in your index.php
1) Revert all the changes you have made to VBulletin in your global.php and init.php etc...
2) set your current working directory to the root directory, you can use chdir('../') at the top of index.php before any require_once
3) require global.php in your index.php require_once('global.php');
Step 2 works because now php now sees the root directory as the working directory.

PHP include path error

I keep getting an error when I try to include simpletest in my include_path:
<?
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(basename(dirname(__FILE__)) . '/../../'));
include 'simpletest/autorun.php';
returns:
.:/usr/lib/php:/Users/kristiannissen/Documents/php
Warning: require_once(/Users/kristiannissen/Documents/php/simpletest/arguments.php): failed to open stream: No such file or directory in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
Fatal error: require_once(): Failed opening required '/Users/kristiannissen/Documents/php/simpletest/arguments.php' (include_path='.:/usr/lib/php:/Users/kristiannissen/Documents/php') in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
personally your view pages are the first executed, such as index.php and view pages should always be in the root of your html.
so within index.php you can do:
define("BASE_PATH",str_replace("\\","/",dirname(__FILE__)));
so now BASE_PATH would be equal to: /Users/kristiannissen/Documents/php/simpletest
So if you want to phpdirectory in your include path you should use dirname() to go UP a directory:
//Get an array of your include paths
$include_parts = explode(PATH_SEPARATOR,get_include_path());
//Extend the paths
$include_parts[] = dirname(dirname(BASE_PATH)); //this is ../../
//recompile the paths and set them
set_include_path(implode(PATH_SEPARATOR,$include_parts));
this is the safe way to accomplish what your trying to do.
There is an alpha2-release on sourceforge that has the arguments.php-require commented out. I guess it isn't needed anymore.

Categories