Improved error handling and testing.

These changes affect both the PHP reader and the C extension.
Previously the PHP reader relied on the poor error handling of
fopen, fseek, etc. Now it does a number of checks before using
those functions.

For all of the errors that we have control over, the PHP reader
and C extension should behave identically.