require_once function error when creating webpage in new folder - php

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.

Related

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

Querying the moodle database within moodle

I am new to moodle. I am trying to query the moodle database using my own PHP scripts. I am using the data manipulation API but i can't seem to fetch anything. What are the steps i should take to accomplish this. I have seen a number of guidelines here and there that I should include the config.php file in moodle so as to access the $DB global variable and hence access the functions of the DML API. The following is my code:
<?php
require '../config.php';
global $DB;
$user= $DB->get_record_sql('SELECT * FROM {mdl_user} WHERE id=?', array(4));
echo mysql_num_rows($user);
?>
I get the following error when I try to run it on TextMate editor:
PHP Warning:  require(../config.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2
PHP Fatal error:  require(): Failed opening required '../config.php' (include_path='.:') in /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2
I will appreciate the help...Thanks.
This error:
PHP Warning: require(../config.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2
means that the script can't find a file called config.php. ../config.php means the script is going to try and open a file in the script's parent directory called config.php. Be sure your script is actually in that location. If it is not, I recommend changing ../config.php to ./config.php and moving your script into the same directory as config.php.
Because your script can't find config.php, it can't connect to the database (config.php should store the credentials, URL, ect.)
I think you are giving wrong path. please be sure that the path you are giving is correct like
require_once('path_from_rootdir to config.php');
note: path_from_rootdir to config.php is the path to config.php and each directory between the path separated by '/'.
There are two issues with this (as wriiten in the question) four lines code of Moodle-
require '../config.php';
Here code is getting error- "PHP Warning: require(../config.php): failed to open stream" because config.php is being looked into the parent directory & file is not there. Please check the path & put the correct path of config.php.
$user= $DB->get_record_sql('SELECT * FROM {mdl_user} WHERE id=?', array(4));
While working with Moodle DML API, table prefix(by default mdl_) must not be added before the table name within {}. Thus, there must be only {user} in place of {mdl_user}.

PHP link not working

I'm trying to require_once a file in a document; here's my current syntax:
$path = $_SERVER['DOCUMENT_ROOT'];
require_once("includes/save-email.php");
The problem is that, when I reload my page in my browser, every element disappears and I get the following two messages:
Warning: require_once(/includes/connect.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '/includes/connect.php' (include_path='.:/usr/lib/php5') in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
I've tried every variation of the link I can think of, including ../link, ./link, /link, and link.
Strangely enough, when I include an element of the layout using the same overall syntax (replacing require_once with include), that element loads without any errors, even though it's in the same directory.
I'm not sure that it matters, but my server is run by 1&1.
require_once(/includes/connect.php) will look for the file in the root of your server not in the current folder
repalace
require_once(/includes/connect.php)
by
require_once(includes/connect.php)
in save-email.php on line 2
setting $path won't do anything. you will need to have the includes folder in the same folder as this php document.

Issues with using PHP Swift Mailer

What I have done:
) Downloaded Swift-4.1.5.tar
) Extracted it
) Uploaded to my host in /public_html/domain/lib using FileZilla
) Made a new script using the code below.
) Opened it in the Browser and I got the following error below
So my question is, how am I getting the error that the file doesn't exist if It's 100% there? Thanks!
<?php
require_once '/public_html/domain/lib/swift_required.php';
?>
Warning: require_once(/public_html/domain/lib/swift_required.php) [function.require-once]: failed to open stream: No such file or directory in /home/myuser/public_html/domain/email.php on line 5
Fatal error: require_once() [function.require]: Failed opening required '/public_html/domain/lib/swift_required.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/myuser/public_html/domain/email.php on line 5
Here's a screenshot of my dir from FileZilla.
http://i.imgur.com/Zsy8y.jpg
This is wrong:
// this is absolute path, just like /home/user or /var/www
require_once '/public_html/domain/lib/swift_required.php';
Use instead:
// this is relative path to the current file
require_once 'public_html/domain/lib/swift_required.php'; //notice: no '/' in the beggining
OR:
// so use this one if you know the whole path to the file
require_once ABSOLUTE_PATH_TO . 'public_html/domain/lib/swift_required.php';
OR:
// or use this one if you don't know the whole path
// or if the path will change (dev machine and production machine)
require_once dirname(__FILE__) . RELATIVE_PATH_TO . 'public_html/domain/lib/swift_required.php';

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