SOLVED: Copy file using code

I am upgrading some v3 code.

Can someone please provide some advice on how to go about copying a file manually in the code. On publish of a dataobject I want to duplicate an existing file to a new filename and also link that new file to another Field in the same dataobject.

The code I have so far creates a new File object in the DB but it always comes up as ‘File cannot be found’ in the asset manager. The columns ‘FileHash’ and ‘FileFileName’ are always blank. The code successfully physically creates a copy of the file on the disk .

$loDraftImage = $this->DraftImage;
if($loDraftImage->ID > 0){
	$lsNewFileName= $loDraftImage->copyFile($loUserFolder->Filename . '1.jpg');
	$loFileLive = Image::find($lsNewFileName);				
		$loFileLive = new Image();
		$loFileLive->Title = 'Image '. $i;
		$loFileLive->ParentID = $loUserFolder->ID;
	$this->LiveImage = $loFileLive;

Can anyone please tell me what I am missing?

After a bit of digging I realised you need to use this function:

$loFileLive->setFromLocalFile($absoluteFilePath, $lsNewFileName);