Check return of fopen() before using it

Fix #9968
Merge pull request #10156 from owncloud/issue/9968

Check return of fopen() before using it