Home » Php » php – what's wrong with my require_once path?

php – what's wrong with my require_once path?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve switched my files over from a local environment to my vps and now my facebook notification isn’t working even thought I’m pretty sure I’ve updated all the paths correctly. I’ve tried writing the require path numerous ways.
I’m doing a “$.post” from jquery to a php page where the facebook notification is sent and am getting this error:

<b>Fatal error</b>:  Class 'Facebook' not found in 

<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9</b><br />

//THIS IS MY PHP REQUIRE PATH. 

require_once('php-sdk/facebook.php') ;

//IN MY LOCAL ENVIRONMENT I WAS USING THIS PATH BECAUSE IT WAS THE ONLY ONE THAT WORKED. THIS DOESN'T WORK ON MY VPS THOUGH. 

require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;
How to&Answers:

I have faced issues like this before, and the best way to handle this is to set your true filepath as a variable & prepend that to your includes/requires. Becuase the whole dirname(__FILE__) setup can act oddly in different environments especially those that use symbolic links. Explicitly stating where files are to be set is the best solution.

So let’s assume this is your codebase path; as per your example:

/home/zjkkvcxc/public_html/

Set that as a variable that all of your pages load in some way like this:

$BASE_PATH = '/home/zjkkvcxc/public_html/';

And now when you make calls to the file system for your app, do this:

require_once($BASE_PATH . 'php-sdk/facebook.php');

What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH to match your local environment. Like this might be a path for a MAMP (Mac OS X LAMP) setup:

$BASE_PATH = '/Application/MAMP/htdocs/';

Regarding how odd __FILE__ can act in symlinked environments, read up here:

Since PHP 4.0.2, _ FILE _ always contains an absolute path with
symlinks resolved whereas in older versions it contained relative path
under some circumstances.

Answer:

You use require_once and have error “Class ‘Facebook’ not found”. If you tried require_once on a file that does not exist, it would cause other error: “Fatal error: require(): Failed opening required ‘php-sdk/facebook.php'”. So the path is probably OK. Check if you uploaded php-sdk properly. The facebook.php might be empty.

Answer:

Your error is :

Fatal error</b>:  Class 'Facebook' not found in 
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9

The valuable information here is :

  • the error occurred because the Facebook class is unknown which means that require_once did not pop this error
  • the error occurred on line 9 of accepted.php

Now you have to look why the class Facebook is unknown on line 9.

  • if you have included the facebook.php before line 9 it is probably not containing the right class. (class names are case sensitive!)
  • if you include facebook.php after line 9 you just have to call it earlier.

PS: posting the first 10-15 lines of accepted.php might give us enough information to pinpoint the exact problem here.

Answer:

Class not found error not refers to problem loading file. You are using a require , if file not exists a require error will be raised and this is not the case.

Probably the class inside facebook.php is not called Facebook, probably is with another name or with other case like “facebook”

Fatal error</b>:  Class 'Facebook' not found in 
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9

Answer:

Is it possible that:

  • your short_open_tag is enabled on your local environment and
  • it’s disabled on your vps and
  • you are using <?instead of <?php in your facebook.php file

Answer:

For some odd reason, i encounter circumstances where php doesn’t find the file i need through conventional means. i found some code used for searching a directory and retrieving a list of files, which was advantageous in a url generation script i was writing. overtime, i have used this simple script for many tasks, such as finding files when normal require/include fails. in a pinch, this hack works for me.

Just fill in the file name $somefile and directory name $log_directory if your directory is in the base path just type it with no slashes. if its below the base path type it like this /path/to/folder

hope this helps.

$somefile = '<!--Filename You need to find-->';
$log_directory = '<!--Directory you want to search-->';
$results_array = array();
if (is_dir($log_directory))
{
        if ($handle = opendir($log_directory))
        {
            while(($file = readdir($handle)) !== FALSE)
                {

                        $results_array[] = $file;

                }
            closedir($handle);
        }
}

foreach($results_array as $value) 
{
    if ($value == $somefile) {

        $url_include = $log_directory."/".$value;

        }else{

        die("error file not found.");

        }
}

require_once("$url_include");

Answer:

require_once JPATH_SITE.'/php-sdk/facebook.php';

Answer:

The require path you said is wrong:

require_once('php-sdk/facebook.php') ;

This need there has a file got full pathname: /dir/to/your/actual/include/path/php-sdk/facebook.php

And later you require file in include_path wrong, dirname(__FILE__) OR __DIR__ is directory of the file where require_once() is called, so you are looking for facebook.php in php-sdk sub-directory under you current php script.

To fix your problem, you have 2 plan

  1. Better one, find your actual include_path and put php-sdk files in, include_path can find from php.ini, or run php -m from shell, or phpinfo(), in this way, your first require_once() will work.

  2. Make sure php-sdk directory includes facebook.php is in same parent directory with your calling php script, this is not common usage, but will make your second require_once() work.

I suggest you to study more on php include_path configure and __FILE__ magic constant.

Answer:

define('DOC_ROOT', realpath(dirname(__FILE__) . '/'));
require_once(DOC_ROOT . '/php-sdk/facebook.php');

Assuming the folder php-sdk is at the root of you website directory.

Answer:

require_once('php-sdk/facebook.php') ;

This assumes that the facebook.php file is in a folder that resides in the current folder of the PHP file beind called.

Is this the case? I suspect not since you have an error!

Are you in a folder up from the root?

—ROOT
—ROOT > FOO –your script here?
—ROOT > php-sdk –facebook in here

If so…

require_once('../php-sdk/facebook.php'); 

would work.

Answer:

Try $_SERVER['DOCUMENT_ROOT'].'/path to /php-sdk/facebook.php'

$_SERVER['DOCUMENT_ROOT'] will give absolute path for www or htdocs folder then you can provide path to facebook.php