/** * Safely opens a PHP stream resource using a filename. * * When fopen fails, PHP normally raises a warning. This function adds an * error handler that checks for errors and throws an exception instead. * * @param string $filename File to open * @param string $mode Mode used to open the file * * @return resource * @throws RuntimeException if the file cannot be opened */ public static function open($filename, $mode) { self::$_open_filename = $filename; self::$_open_mode = $mode; self::$_open_ex = null; set_error_handler(array('puzzle_stream_Utils', '___error_handler')); $handle = fopen($filename, $mode); restore_error_handler(); if (self::$_open_ex) { throw self::$_open_ex; } return $handle; }