简体   繁体   中英

ERROR 500 Require_once

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?

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 . ie 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!

Try

echo getcwd();

to find the working path to troubleshoot any path related issues.

你可以试试

<?php require_once(dirname(__FILE__) . "php/Classes/User/UserDataBaseManager.php");?>

I checked Apache error.log and found an issue with my dbname in dbconn.php ,

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [1049] Unknown database 'study_db' 

Then I corrected database name and it started working.

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