Search code examples
phphandlerfile-get-contents

Global error handling of file_get_contents() PHP


I am occasionally getting errors while using file_get_contents and its used a fair bit in my script. I know I can suppress errors individually with @file_get_contents and that I can set global error messages with

//error handler function
function customError($errno)
  {
  echo 'Oh No!';
  }

//set error handler
set_error_handler("customError");

But how do I specifically set an error handler for all file_get_content's uses?

Thanks


Solution

  • you may set your custom error_handler before invoking file_get_contents and then use restore_error_handler() function right after file_get_contents. if there is multiple usage of file_get_contents in your code, you may wrap file_get_contents by some custom function.