PHP: fopen error handling

You should first test the existence of a file by file_exists().

or simple solution without exceptions: