Trouble with path for require function in PHP with o2switch host - php

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.

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

require/include throwing warning: no such file or directory in

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:

required_once issue in Google API Client for PHP

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());

Can't get require_once called by ajax on localhost to work

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

Troubleshooting PHP Login connection

I have encountered a problem that I have not come accross yet when setting up a log in page using php.
The page has a error message that relates to line 1 ( require_once('../Connections/Login.php)
that states
[function.require-once]: failed to open stream: No such file or directory
Fatal Error: require_once() [function.require]: failed opening required ... (include_path='.:/usr/share/pear-php5')
I know it is probably really simple and stupid, but I'm really struggling to self-troubleshoot and would really value some help please?
This is where I have been so confused. I also thought the same, that it just couldn't find the file; however the structure is just as you have said:
htdocs/comparison.php (the log in page)
htdocs/Connections/connComparisonLogin.php
Sorry this is going to confuse you, I simplified the actual script in my original question and here is the actual error message:
Warning: require_once(../Connections/connComparisonLogin.php) [function.require-once]: failed to open stream: No such file or directory in /home/fhlinux135/s/suzukigenuine.com/user/htdocs/comparison.php on line 1
Fatal error: require_once() [function.require]: Failed opening required '../Connections/connComparisonLogin.php' (include_path='.:/usr/share/pear-php5') in /home/fhlinux135/s/suzukigenuine.com/user/htdocs/comparison.php on line 1
Have I done it all wrong?
I think your require statement should be:
require_once 'Connections/connComparisonLogin.php';
Where is your "Connections/Login.php" relative to the currrent php file.
php will look for the file relative to the current file, or, in one of the directories specified in the "include_path" php.ini setting.
The error just means php can't find the Login.php file. Assuming the page you are calling is index.php you should have a director structure like
-Dir1
|-index.php
-Connections
|-Login.php
Why are you going up a directory in your require statement? Remove the "../" from the beginning of your require_once path.

Categories