ERROR 500 Require_once

user1305760 picture user1305760 · Apr 19, 2012 · Viewed 16.7k times · Source

this is my piece of php code:

<?php
require_once 'php/Classes/User/UserDataBaseManager.php';
require_once 'facebook/src/facebook.php';
?>

And when I try to browse this code in http:/mydomain/myfile.php then the browser return an error 500.

Apache log says:

[Wed Apr 18 20:38:07 2012] [error] [client 190.50.81.189] PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'php/classes/DataBase/DataBaseManager.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/virtuales/blyxo.com/php/Classes/User/UserDataBaseManager.php

Solutions?

Answer

jcbwlkr picture jcbwlkr · Apr 19, 2012

What this looks like to me is that the file UserDatabaseManager.php is trying to include DataBaseManager.php but it is providing the wrong path. Your apache error indicates that it will first look in . i.e. the current directory then /usr/share/pear then /usr/share/php

Your DataBasemanger.php file is not in either of the last two. When it attempts to look in the current directory it is including your relative path. So when your UserDataBaseManager.php file (which is in php/Classes/User/) provides that path then apache ends up looking for your DataBaseManager.php file in php/Classes/User/php/classes/DataBase/DataBaseManager.php

There are several ways to approach this. You could get the document root and prepend it to every include like this

$root = $_SERVER['DOCUMENT_ROOT'];
require_once($root . "/php/classes/DataBase/DataBaseManager.php");

Or if this app is in a controlled environment and you can edit the php.ini file you can define the include path to also look in /var/www/virtuales/blyxo.com/php/Classes/ and just include the file with

require_once("Database/DataBaseManager.php");

A better solution might be to define an __autoload function that will be called every time you try to instantiate a new object and have it use some logic to look for the file to include. This solution would take a bit more restructuring but you might consider it. Here is the doc on defining your own auto loader http://us2.php.net/manual/en/function.autoload.php

I know there are other ways to do this. Let me know what works for you!