Fixed a bug with file wrtiting

- PHP was trying to close the file if it hadn't been opened