You must use `fopen($file, 'w');`, because `a` appends to a file. If the file does not exist, use w (write). Also, to determine the directory, use a PHP env var, don't ask a user for it.