I am trying to use the code require $_SERVER['DOCUMENT_ROOT'].'/resources/mysql.php'; to include the file /resources/mysql.php (relative to root).
On my computer this works, and there are no problems, however my website users have complained that EVERY page except the root (/index.php) fails to load with the error
Fatal error: require() [function.require]: Failed opening required '/home/website/public_html/resources/mysql.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/website/public_html/website/events/index.php on line 2
Why is this failing only on some computers. I have tried to replicate the problem on my computer and even after clearing my browser and DNS cache I cannot get this error to occur. What am I doing wrong and is there a better way to dynamically include php files?
This looks weird that you can access the website but some people not.
You could use ( if you are in php > 5.3 ):
__DIR__
You could also use:
dirname(__FILE__);
Be aware that DOCUMENT_ROOT is not available ( i think ) when you are running your script through CLI
Related
When creating a scheduled task to run a php script in Plesk Onyx on windows it results in an error.
However when i run the same script in the browser it works without any issues.
I have been looking for the permission settings in the webroot and set them to allow access to all user groups on the server.
The error i get is the following:
Warning: require(\pcp2\inc\db_config.php): failed to open stream: No such file or directory in D:\www\domain\pcp2\conversion\addBooking.php on line 5
Fatal error: require(): Failed opening required '\pcp2\inc\db_config.php' (include_path='.;.\includes;.\pear') in D:\www\domain\pcp2\conversion\addBooking.php on line 5
Line 5 contains the following info:
require($_SERVER['DOCUMENT_ROOT']."\pcp2\inc\db_config.php");
It's failing becasue $_SERVER['DOCUMENT_ROOT'] is a value provided by the web server, and is thus undefined when run without a web server (i.e., from the command line.) You'll need to provide an alternative mechanism to set the base directory.
You might use relative paths:
require("pcp2\inc\db_config.php");
Or absolute paths based on the magic constant __DIR__. (This assumes the script doing the require'ing is in the document root directory.)
require(__DIR__."\pcp2\inc\db_config.php");
Ideally however, you're better off using PSR-4 namespacing with an autoloader.
I'm getting this error when my site is online
[host www.sitename.com] Backend fatal error: PHP Fatal error: require_once() [function.require]: Failed opening required 'admin/config/connection.php' (include_path='.:/opt/cpanel/ea-php53/root/usr/share/pear:/opt/cpanel/ea-php53/root/usr/share/php') in /home/mistrybu/public_html/index.php on line 373\n
The site works well in localhost but when its put online , the dynamic part is not showing.
I changed the php version from php7 to php5 in cpanel. Now the front end is working but the backend is not.
Can someone help me solve this error?
PHP version
Definitely do not go back to PHP5. Keep it to 7. If that means rewriting some of the existing code, so be it, it will make your site more secure and more future proof.
Error Message
Your require_once() method cannot find or access a particular file. This is most probably because:
File missing
The file (connection.php) doesn't exist in the given location (admin/config). Now keep in mind that as the location doesn't start with a /, it's going to be relative to where the require_once() method is being run from.
Since you're running require_once() from:
home/mistrybu/public_html/
it's going to look for the file here:
/home/mistrybu/public_html/admin/config/.
Does that location exist? Does the connection.php reside in the folder?
Folder mismatch
Try adding the following prefix to your path, and see if that makes a difference:
require_once $_SERVER['DOCUMENT_ROOT'].'/admin/config/connection.php';
File errors
Maybe the file exists, but for some reason, gives an error. Try running the following from your command line and see if you get any errors:
php -f admin/config/connection.php
I have recently been trying to add PHPMailer script to my site, following the accepted answer from here:
Send attachments with PHP Mail()?
I have never used a require statment before so I assume I must be doing it wrong, currently testing on my local host version of the site. I have downloaded the entire zip of git (unzipped it) and moved it to the dir my scripts are in.
In my script I added the following line:
require_once(__DIR__.'/PHPMailer-master/class.phpmailer.php');
Which to my understanding was all I needed to do to use it. However I get this error:
Warning:
require_once(C:\xampp\htdocs\Website\PHPMailer-master\class.phpmailer.php):
failed to open stream: No such file or directory in
C:\xampp\htdocs\Website\include\mailer.php on line 10
Fatal error: require_once(): Failed opening required
'C:\xampp\htdocs\Website\PHPMailer-master\class.phpmailer.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\Website\include\mailer.php on line 10
Any ideas what is causing this error? Am running PHP 5.6.21 if this is relevant.
Try to use the full path to the file, using realpath() - http://php.net/realpath, and use dirname(__FILE__) to get your current directory:
I'm almost going crazy here.
I have a simple site locally with a contact form functionality that works perfect with just PHP.
Now I want to call it via AJAX and I always get errors because of the relative paths.
The problem is, that the php file called by ajax has two require_once, which won't work then.
I know that I should use absolute paths to get it working and I tried different approaches suggested by different posts, but all result in an error.
I tried using
require_once __DIR__ . 'app/config/settings.php';
I used define('ROOT_SYS',dirname(__FILE__).'/'); in the index.php making the ajax call and having the require in the php file like this:
require_once ROOT_SYS . 'class/model/contactForm.php';
and many other approaches like recommended in this question PHP include best practices question, without success. I get them all to work php side, but when I use ajax it always results in an error like:
Notice: Use of undefined constant ROOT_SYS - assumed 'ROOT_SYS' in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Warning: require_once(ROOT_SYSclass/model/contactForm.php): failed to open stream: No such file or directory in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Fatal error: require_once(): Failed opening required 'ROOT_SYSclass/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
OR
Warning: require_once(app/class/model/contactForm.php): failed to open stream: No such file or directory in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Fatal error: require_once(): Failed opening required 'app/class/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Either a constant is undefined, because it wasn't included or opening a stream failed.
I mean there must be way to get it working with having a require_once in the calles PHP file.
Do you have any experiences you can share with me? I'm really desperate right now.. :(
My folder structure is like this:
app
-class
--controller
--helper
--model
--view
-config
-template
docroot
lib
system
-css
-js
index.php
My project path on localhost:
G:\xampp\htdocs\website.de
http://localhost/website.de/
My ajax call
$.ajax({
type: "POST",
url: "app/class/controller/contactForm.php",
data: content
})
And the requires in the called contactForm.php how I had them from the start on.
require_once 'app/class/model/contactForm.php';
require_once 'app/config/settings.php';
There must be some way to get it working, preferably with a setup that works locally and on a webserver.
I know this is a lot to read, so thank you for your time!!
Using __DIR__ is the way to go. You should elaborate on that statement:
require_once __DIR__ . 'app/config/settings.php';
I guess you are just missing the / because __DIR__ will give you a path without the trailing /. It should be:
require_once __DIR__ . '/app/config/settings.php';
Here is the first error:
Warning: require(/hb/includes/constants.php) [function.require]: failed to open stream: No such file or directory in C:\wamp2\www\hb\includes\connection.php on line 2
And the second error that immediately follows:
Fatal error: require() [function.require]: Failed opening required '/hb/includes/constants.php' (include_path='.;C:\php\pear') in C:\wamp2\www\hb\includes\connection.php on line 2
Here is the line that it doesn't like:
require("/hb/includes/constants.php");
I did install a new version of WAMP, but I know the directories/files are fine and the code worked previously and on another server. It seems like none of my require()'s are working anymore. I tried looking around and some suggested to others that they edit their php.ini, but I don't know what directory I would point to - if you can't tell I'm pretty noobish regarding all things server related.
Any help is appreciated.
First of all, are your really -- really ! -- sure that the file /hb/includes/constants.php, that your are trying to include, exists ?
Considering that the path you requested begins with a /, it looks like you're using an absolute path... that looks like an UNIX path, but you are on a Windows server...
If it exists, and PHP still cannot include it, check :
that it is readable by your webserver (including the directories) -- I'm thinking about UNIX permissions
that there is no open_basedir restriction that would prevent PHP from accessing those files.
BTW, having a warning and a fatal error is the behavior that's to be expected when using require : the warning is normal (it comes from include) ; and the fatal error is the difference between include and require.