PHP require_once '...........'; - php

I am calling the function require_once '../autoload.php'; but I get the error
Warning: require_once(../autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
Fatal error: require_once(): Failed opening required '../autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
What am I doing wrong?

To avoid this type of problems, and for best practice, i advice to use the root path:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
The code above will assign to $root your realpath on the server.
After that just use the require/include etc according to your localhost/webserver root :
require_once($root/Api/Autotask/vendor/autoload.php)
For example, if your file is located in websiterootfolder/php/nice_folder
Your code will be :
require_once($root/php/nice_folder/autoload.php)
By using this practice, you can include any file from whatever position and any way you want, dynamic or not

Related

include path is not working when referencing another file

I'm having problem with include() and require_once() this is what I have on my script on report-publisher file that resides here :
C:\xampp\htdocs\checklists\reports\report-publishers.php
now i want to include this file:
<?php include("images/report-publisher-completed.php");?>
and the directory of that script is here:
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
it works the problem is in this report-publisher-completed.php i have the following:
require_once("../../includes/database.php");
here is the location of that file: C:\xampp\htdocs\checklists\includes\database.php
so when i run the code I get the following error:
( ! ) Warning: require_once(../../includes/database.php): failed to
open stream: No such file or directory in
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
on line 3
( ! ) Fatal error: require_once(): Failed opening required
'../../includes/database.php' (include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
on line 3
How can I fix this issue?
Thanks!
Try like this
require_once(dirname(__FILE__).'/../../includes/database.php');
The safer way to include a file is to use a relative path from the current script, using the constant __dir__, like this :
include __dir__ . '/includes/database.php';
So, for your case, probably :
include __dir__ . '/../../includes/database.php';
Try this
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/database.php";
Instead of
require_once("../../includes/database.php");

PHPUnit Fatal error: require() when running test

I'm new at PHPUnit and I'm having a problem when running the test.
I noticed that the problem was that I was not including the class I supposed to test so I put the following line:
require '../../files/Cliente.php';
But then it gaves me the next error:
PHP Warning: require(../../files/Cliente.php): failed to open stream: No such file or directory in /home/esperanza/Escritorio/clasificadosj-clasificados-7da0b31e6060/pruebas/files/ClienteTest.php on line 6
PHP Fatal error: require(): Failed opening required '../../files/Cliente.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/esperanza/Escritorio/clasificadosj-clasificados-7da0b31e6060/pruebas/files/ClienteTest.php on line 6
My directory structure is the next:
Project
|_files
|_Cliente.php
|_Test
|_ClienteTest.php
I don't know if I have to configure a XML document or a bootstrap first.
Thanks a lot!
The problem is that you require a file that does not exists. As require will give a fatal error when a file is not found, you get that fatal error. Compare with: Difference between "include" and "require" in php.
Check the path to the file you want to require and correct it:
require __DIR__ . '/../files/Cliente.php';
Make use of the __DIR__ magic constant so that it's easy to specify in a relative fashion to the php-file of the testcase you write while the path is absolute. You then prevent to use the include_path configuration directive which makes it easier to trouble-shoot.

PHP include file in class, wrong paths

I have class called client, and there i have function, in which i'm trying to include/execute php file, but when i'm doing it all path's in that file which i'm trying to include goes wrong.
class function:
public static function SendSSHCommand($ssh_cmd) {
include('../Core/core_class.php');
$ssh = new samp_panel();
$ssh->SendCommand($ssh_cmd);
}
I would like get a solution, how to include file, that all directory path's in that file wouldn't go wrong.
I get many errors like that:
Warning: include(Net/SSH2.php): failed to open stream: No such file or directory in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Warning: include(Net/SSH2.php): failed to open stream: No such file or directory in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Warning: include(): Failed opening 'Net/SSH2.php' for inclusion (include_path='.:/usr/share/pear:Core/NetModule') in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Fatal error: Class 'Net_SSH2' not found in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 366
I always change my include directory when I run a script. For me it just makes it easier if I set a point of reference for all my included files.
http://php.net/manual/en/function.set-include-path.php
Put something like this at the top of your file
set_include_path('/usr/www/2/main');
include('include1.php');
include('subdir_of_include_path/include2.php');
There are several options but this is the one I find useful with my projects. This way I force and know what my include path is.

require_once(dirname(__FILE__)): Failed to open stream

There may be a super easy fix to this that I am completely missing. I've looked around Stack Overflow and other answers say that what I am doing now should work.
I am trying to require a config.php so I can get the credentials to a database. I am requiring it via require_once (dirname(__FILE__) . "/../../config.php"); The file that I am referencing the method from (config.php) is located at /internal/CloudShop/login config.php is located at /internal/config.php But, when I load the page, I get this error
Warning: require_once(C:\xampp\htdocs\CloudShop\internal\CloudShop/../../config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\CloudShop\internal\CloudShop\login.php on line 48
Fatal error: require_once(): Failed opening required 'C:\xampp\htdocs\CloudShop\internal\CloudShop/../../config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\CloudShop\internal\CloudShop\login.php on line 48
What am I doing wrong?
The script where you execute require_once(...) is in /internal/CloudShop. You are looking for a file that is in /internal. Therefore you need to go only 1 directory above in the hierarchy, not two.
require_once (dirname(__FILE__) . "/../config.php");
will do the trick.

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