简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM