error handling added in case file_put_contents is failing

refs #4011 

@bantu now with tests - I'll us e this approach for #4969 as well

@icewind1991 @bartv2 THX