PHP include file in class, wrong paths - php

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.

Related

PHP require_once '...........';

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

Problems when including a PHP class into an included PHP file that is called by Ajax

My live setup is as follows:
I have an Ajax call to a PHP file ("ajax.php") on a server which I have been using and it works fine.
The php file is now for the first time instructed to include a second PHP class within one of its functions:
if ($stmnt) {
require_once('path/to/statement.class.php');
$statement = new STATEMENT();
}
This second class ("statement.class.php") also includes a third class within the same folder as the second class and attempts to extend it:
require_once('tcpdf_include.php');
class MYPDF extends TCPDF {...}
class STATEMENT {
public function __construct() {...}
}
I have been running lots of tests on the statement class, and when I call it directly from the browser, I get no errors. It properly includes the tcpfd class and extends it and I can work with it as expected.
However, when using above mentioned Ajax call to instruct "ajax.php" to go as an inbetween, suddenly, the server claims that it cannot find the file "tcpfd_include.php" for inclusion as an error within "statement.class.php" and therefore cannot instatiate or extend its class TCPDF:
I get the following error when doing a simple require_once('tcpdf_include.php');:
Fatal error: Class 'TCPDF' not found in /home/[userxyz]/public_html/[ajax.php path]/path/to/statement.class.php on line 10
(line 10 being where I'm trying to extend the class, which means that it didn't complain about an unsuccessful include, but about an unsuccessful extension of the class that should be included by now)
if I change the inclusion to say include('tcpdf_include.php') or die ('dead');, I get a different error message returned to me via ajax:
Warning: include(1): failed to open stream: No such file or directory in /home/[userxyz]/public_html/[ajax.php path]/path/to/statement.class.php on line 4
Warning: include(1): failed to open stream: No such file or directory in /home/[userxyz]/public_html/[ajax.php path]/path/to/statement.class.php on line 4
Warning: include(): Failed opening '1' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[userxyz]/public_html/[ajax.php path]/path/to/statement.class.php on line 4
Fatal error: Class 'TCPDF' not found in /home/[userxyz]/public_html/[ajax.php path]/path/to/statement.class.php on line 10
This now leads me to believe that the relative path somehow got screwed up, so I tried:
echo (file_exists($_SERVER['DOCUMENT_ROOT'].'/[ajax.php path]/path/to/tcpdf_include.php')?"true":"false");
right above the troublesome include and it spits out true, even via Ajax.
But even if I change the include to read:
include_once ($_SERVER['DOCUMENT_ROOT'].'/[ajax.php path]/path/to/tcpdf_include.php') or die ("file doesn't exist");
I get the same error, that the file doesn't exist, failed to open stream, blah blah.
What am I missing here?
Okay, I finally solved the problems after a few wasted hours:
It turns out that #Ryan's answer was at the gist of it the solution. However, I was searching for the problem in the wrong place.
I included B.php into A.PHP and C.PHP into B.PHP and when this didn't work, I falsely assumed that C.PHP was not included correctly into B.PHP. I didn't consider that the Class I was trying to extend in B.PHP was not actually inside C.PHP but inside D.PHP or E.PHP, since the TCPDF class did its own including with relative paths. And by trying to include the whole shebang from a directory outside the folder where C.PHP was, I caused issues with sub-inclusion within the whole TCPDF package.

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.

PHP link not working

I'm trying to require_once a file in a document; here's my current syntax:
$path = $_SERVER['DOCUMENT_ROOT'];
require_once("includes/save-email.php");
The problem is that, when I reload my page in my browser, every element disappears and I get the following two messages:
Warning: require_once(/includes/connect.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '/includes/connect.php' (include_path='.:/usr/lib/php5') in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
I've tried every variation of the link I can think of, including ../link, ./link, /link, and link.
Strangely enough, when I include an element of the layout using the same overall syntax (replacing require_once with include), that element loads without any errors, even though it's in the same directory.
I'm not sure that it matters, but my server is run by 1&1.
require_once(/includes/connect.php) will look for the file in the root of your server not in the current folder
repalace
require_once(/includes/connect.php)
by
require_once(includes/connect.php)
in save-email.php on line 2
setting $path won't do anything. you will need to have the includes folder in the same folder as this php document.

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