If the file is not created correctly, perhaps this should not be executed? 
Suggestion: add an  if ( $ret ) condition before this line