Question

I'm trying to set up some error logging

register_shutdown_function('shutdown');

but whenever I try to include a file inside of the shutdown function - it cannot be found, even though it exists and is included just fine throughout my application elsewhere

function shutdown()
{
    require_once LIB_DIR.'phpmailer/class.phpmailer.php';
}

Gives me an error saying the file could not be found.. am I missing something here, does the relative path to a file change inside the shutdown function or something?

Was it helpful?

Solution 3

One of your comments suggest you're using a relative path:

../web-app/app/libraries/phpmailer/class.phpmailer.php
^^

That's generally unreliable in the PHP universe but in this particular case:

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

If the error message doesn't display the full path, you can at least print current directory yourself for debugging purposes:

var_dump(getcwd());

OTHER TIPS

As PHP doucmentation in Notes section:

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

I'm not sure but maybe It makes that the file could not be found.

Use ABSOLUTE paths to files instead of relative in shutdown functions. Because when script processing is complete current working directory changes.

I found the solution in this thread Write to file with register_shutdown_function

I needed to set a working directory constant before the function is called and then switch back to that directory inside the shutdown function.

define('WORKING_DIRECTORY', getcwd());

function shutdown()
{
    chdir(WORKING_DIRECTORY);
    //files can now be included relative to the application
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top