PHP Fails on require_once

10,485

Solution 1

To enable error reporting:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("std/classes.php");
?>

Hopefully that should work.

EDIT: If this does work, remember to turn off display errors before putting anything in a live, public facing environment!

Solution 2

You can temporarily turn error reporting back on using the error_reporting() function, for example to show all errors put the following code in your file:

error_reporting(E_ALL);

Of course, to change this permanently you should edit your php.ini file, and make sure you have error_reporting enabled as well as display_errors (at least if this is not a production environment). You can also try:

ini_set('display_errors', 1);

Though this may not work if you have a fatal error on the page. Again, to enable this permanently you will have to modify your php.ini file.

It is generally suggested that you enable display_errors only on non-production systems so that users don't get potentially sensitive information via your error messages.

In either case, you should be able to find the php errors in the apache error log, on ubuntu this is located here:

/var/log/apache2/error.log

Though it may vary based on your distribution.

Share:
10,485
DigitalJedi805
Author by

DigitalJedi805

Software engineer from Central California, self taught since 2000, college educated since 2008, industry experience since 2010.

Updated on June 13, 2022

Comments

  • DigitalJedi805
    DigitalJedi805 almost 2 years

    I've got a PHP script that includes ( or 'requires' ) a set of other scripts. This is effectively for importing all of my classes. What I'm encountering is an HTTP 500 error. I've sifted through and commented out code piece by piece to determine that it's failing on the require_once in one of my files.

    Here is the code:

    index.php:

    <?php
    require_once("std/classes.php");
    ?>
    

    And std/classes.php:

    <?php
    RequireStandards();
    RequireAddons();
    
    function RequireStandards( )
    {
        $ClassFiles = scandir("classes/standard");
    
        foreach( $ClassFiles as $ClassFile )
        {
            if( $ClassFile == "." || $ClassFile == ".." )
                continue;
    
            //require_once("classes/standard/" . $ClassFile );
        }
    }
    
    function RequireAddons()
    {
        $ClassFiles = scandir("classes");
    
        foreach( $ClassFiles as $ClassFile )
        {
            if( $ClassFile == "." || $ClassFile == ".." || $ClassFile == "standard" )
                continue;
    
            //require_once("classes/" . $ClassFile );
        }
    }
    
    ?>
    

    This code will work as it sits, but as soon as I uncomment the requires, it fails. What strikes me as odd is that I have plenty of other sites on this server that act in almost an identical manner.

    I feel as if I somehow have my PHP error reporting turned off... which I don't know how to turn back on; as I just upgraded to PHP 5.3. I would typically expect a 'couldnt open file' or some such in my browser, if PHP failed.

    Maybe someone could tell me why this is kicking back an HTTP 500, or perhaps just how to re-enable error reporting. It would be much appreciated; this just doesn't seem to make much sense.

    • miku
      miku about 12 years
      Can you consult your server logs?
    • Jim D
      Jim D about 12 years
      Is your class folder in the root directory? Try adding a "/" before classes? Just a guess.
    • Alon Eitan
      Alon Eitan about 12 years
      maybe a problem with your include path php.net/manual/en/function.set-include-path.php
    • kpotehin
      kpotehin about 12 years
      It's better to use $_SERVER['DOCUMENT_ROOT'] . '/classes/standard/;
    • DigitalJedi805
      DigitalJedi805 about 12 years
      These are all valid comments - it turned out that the relative paths that were working fine in XAMPP did not translate so well over to IIS. After re-enabling php error reporting, I managed to figure out which these were, and correct them. It seems that file IO changed a bit from 4.?.? to 5.3.
  • DigitalJedi805
    DigitalJedi805 about 12 years
    Thank you. This is the answer I was hoping to get. While @Madbreaks' answer was 'an answer' - it was not an answer to anything I asked. I will take your advice Madbreaks, and build an AutoLoad, but rather than answering with 'this is how you should write your code', can people start answering the real issue? It's quite the irritant to get 'rewrite your code' as a response to 'I know this is valid php why isnt THIS code working'. Thank you either way though. As for Jim D - this put me back on top of my actual PHP errors and I can fix a couple of 'failed to open stream's now. Much appreciated.
  • Jim D
    Jim D about 12 years
    @DigitalJedi805 - Glad I could help! The ini_set('display_errors') bit is something I'll generally toggle based on a production/environment constant. That way, I can't be clumsy and accidentally forget to turn it off.