Use a local temp file when replication

The previous commit to this issue caused problems with file creation and
permissions. This changes switches to using temp files in the same dir as the
target destination to fix the file permission problems. All files are now also
handled the same way and directories created when necessary.

[#78901290]