I have been trying to import/include file in my php script file and somehow it is not working. I know this require_once question has been asked so many times [ 1. require_once with subfolders , 2. require_once() cant find the include path, 3. Using require_once for up directory not working] but none of them seemed to be working for me.
Blow picture will explain what I am trying to do clearly:
What I tried and errors:
Attempt 1
require_once(__DIR__.'GoogleClientApi/src/Google/Service/Analytics.php');
echo 'Hey1'; //Does not echo this, means something went wrong above.
require_once(__DIR__.'GoogleClientApi/src/Google/Client.php');
echo 'Hey2';
Error I get:
Attempt 2 (Fixed issue of attempt 1 but raised another issue.)
So I decided to use absolute path :
include '/Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php';
echo 'Hey1';
include '/Applications/MAMP/htdocs/GoogleClientApi/src/Google/Service/Analytics.php';
echo 'Hey2';
Gives me error in Client.php file:
`Warning: require_once(/Google/Auth/AssertionCredentials.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php on line 18`
`Fatal error: require_once(): Failed opening required '/Google/Auth/AssertionCredentials.php' (include_path='.:/Applications/MAMP/bin/php/php5.5.10/lib/php') in /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php on line 18`
Investigating upon I found Client.php (of Google APIs Client Library for PHP) has these lines which are actually present:
require_once 'Google/Auth/AssertionCredentials.php'; // <-- Can't find
require_once 'Google/Cache/File.php'; // <-- Can't find
require_once 'Google/Cache/Memcache.php'; // <-- Can't find
I do not what is going wrong here. Fixed attempt 1's issue but now errors are in google's own library which I do not know how to overcome. I assume I am doing something silly which I can not figure out. Any help or suggestion will be appreciated.
Did you ever dumped __DIR__? Usually there's no / at the end. So you would have to change your code to this:
require_once(__DIR__.'/GoogleClientApi/src/Google/Service/Analytics.php');
^
I just checked in your screen shot with the error message, there's no / at the end.
/Applications/MAMP/htdocsGoogleClientApi/src/Google/Client.php
instead of /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php
It turns out I had to set the include path to include the root directory (src folder) using this line :
set_include_path("/Applications/MAMP/htdocs/GoogleClientApi/src/");
This worked for me in Client.php
set_include_path(__DIR__.'/../' . PATH_SEPARATOR . get_include_path());
Related
I have uploaded website to o2switch host.
When I worked on localhost, all worked well.
But on o2switch, I have this error :
Warning: require_once(models/shopManager.class.php): failed to open stream: No such file or directory in /home/myUserNameo2switch/myDomain.fr/controllers/ShopController.controller.php on line 2
Fatal error: require_once(): Failed opening required 'models/shopManager.class.php' (include_path='.:/opt/alt/php73/usr/share/pear') in /home/myUserNameo2switch/myDomain.fr/controllers/ShopController.controller.php on line 2
I use MVC model with file structure like :
index.php
-controllers
-ShopController.controller.php
-models
-shopManager.class.php
In my index.php I do :
require_once "controllers/ShopController.controller.php";
$shopController = new ShopController();
In ShopController.controller.php, I do :
require_once "models/shopManager.class.php";
On o2switch PHP setting (I think similar to php.ini) there is configuration for include_path equal to .:/opt/alt/php73/usr/share/pear
The trouble is it coming from include_path value ?
That's the first time I work on online website and my first question here. Sorry if I am clumsy. Thank you
In ShopController.controller.php, you can try to change
require_once "models/shopManager.class.php";
to
require_once "../models/shopManager.class.php";
to see if the error message would disappear.
Thanks ild flue for your answer. It doesn't work.
My trouble is fixed. I had a trouble with capital and not capital letter between file and file called on require_once... :(
The most surprising is I had no error on local.
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';
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');
I'm getting a rather frustrating warning which is blocking me from further development.
I've tried using require_once / include_once for a file with a relative path, and it seems to be unable to find it. Then all of a sudden it works, and now it doesn't work.
What could be causing this problem?
Detailed screenshot of my file and paths:
Notice that line 3 works, but line 4 doesn't work.
Warning I'm getting client side:
Thanks.
Update with a screenshot of my finder:
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';