Use php require for local directory - 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:

Related

No such file or directory error on require_once in php

I am trying to make a login system for my app, and needed to use php for it. However I'm really new to php so I got errors. First of all, I am using a free web hosting for now, so it might be the issue here. That being said, here is the error part of my php:
<?php
require_once 'include/DB_Functions.php';
?>
And it gives me this error:
Warning: require_once(include/DB_Functions.php): failed to open stream: No such file or directory in /srv/disk3/1965965/www/yigittest.atwebpages.com/login.php on line 2
Fatal error: require_once(): Failed opening required 'include/DB_Functions.php' (include_path='.:/usr/local/php-5.4.45/share/pear') in /srv/disk3/1965965/www/yigittest.atwebpages.com/login.php on line 2
However, my include folder contains all the required php files. So what might be the issue here?
Try this demo code or plz share your folder structure
require_once(__DIR__.'/../include/DB_Functions.php');

how to correctly include a file in php for cronjobs and for normal execution

So I have a script that updates all currency rates for my webshop.
I am trying to have it run using a cronjob but when it's executed through a cronjob I get this error:
PHP Warning: require(.../.../core/dbcon.php): failed to open stream: No such file or directory in /var/www/domain.com/wholesale/system/labs/cur.php on line 2
PHP Fatal error: require(): Failed opening required '.../.../core/dbcon.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/domain.com/wholesale/system/labs/cur.php on line 2
I've also tried using the full link to include it but that seems to not work when running it manually and will also not work in cronjobs.
In the php file itself this is what I use to include it:
require(".../.../core/dbcon.php");
And when I manually run it, it returns all the currencies right, and updates them in the database.
How would I correctly require the file so it can execute using a cronjob and manually as well.
Try require("./../../core/dbcon.php");
or use absolute paths.
UPDATE
Answer has been updated - see comments below for more info.

mediawiki Fatal error: require_once(): Failed opening required after server moved

When I moved MediaWiki from Localhost to the server I started getting the below error. I am using the latest version and tried to use php 5.3, 5.4 and 5.5 but error exists for all version of php. I know this might not be php version related but just cross checked.
Warning:
require_once(/home/meuser/public_html/site_dir\extensions\Cite\Cite.php):
failed to open stream: No such file or directory in
/home/meuser/public_html/site_dir/LocalSettings.php on line 132
Fatal error: require_once(): Failed opening required '/home/meuser/public_html/site_dir\extensions\Cite\Cite.php'
(include_path='.:/opt/php54/lib/php') in
/home/meuser/public_html/site_dir/LocalSettings.php on line 132
When I commented Cite extension (to disable the extension) to cross check, it gives the same error for the next extension. So I believe there is something wrong in moving server?
I have just uploaded all files to the server and changed database and site url settings in Localsettings.php and imported database.
Is there any extra step required?
There's an issue with the directory separators in your paths. Notice how they're all forward slashes until you reach site_dir. Fix these and it should work perfectly. Based on this code the paths you need to change are in LocalSettings.php
i think this will help you
Syntax:
require_once($_SERVER['DOCUMENT_ROOT']./your file location with only '/' slash);
ie.,
require_once($_SERVER['DOCUMENT_ROOT']./site_dir/extensions/Cite/Cite.php);

require_once can't find file on webserver but works on localhost

All,
I have the following files:
builder.php
Oauth.php
twitteroauth.php
Within my builder.php page I have the following code:
require_once('twitteroauth.php');
Then in my twitteroauth.php page I have the following code:
require_once('OAuth.php');
All of these files are in the following path: wp-content/themes/alyeska/framework/frontend/functions
I'm testing this on my localhost and it works fine. However, when I upload all of these files to my webserver I get the following error message:
Warning: require_once(OAuth.php) [function.require-once]: failed to open stream: No such file or directory in /home/arokyne/website.com/wp-content/themes/alyeska/framework/frontend/functions/twitteroauth.php on line 10
Fatal error: require_once() [function.require]: Failed opening required 'OAuth.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/arokyne/website.com/wp-content/themes/alyeska/framework/frontend/functions/twitteroauth.php on line 10
I'm not sure why I'm getting these errors since it works on my localhost. Can anyone point me in the right direction on how to fix this issue?
Thanks!
Two things jump to mind- make certain your path is correct, including the case sensitivity; also, make certain that the permissions on the file allow for the php server to read the file. A 644 mode should be sufficient, I believe; but, worst case, a 755 would definitely work.
And finally you may want to check whether your include path is correct.

Two Errors trying to include .php file: Warning: require(...) AND Fatal error: require() ... (include_path='.;C:\php\pear')

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.

Categories