$p = $new_file;
         $deletion_array[] = $p;
         $replaced_file = true;
     } else {
         if ($useoriginal == 'yes' && resource_download_allowed($ref, '', $result[$n]['resource_type'])) {
             // this size doesn't exist, so we'll try using the original instead
             $p = get_resource_path($ref, true, '', false, $result[$n]['file_extension'], -1, 1, $use_watermark);
             $pextension = $result[$n]['file_extension'];
             $subbed_original_resources[] = $ref;
             $subbed_original = true;
         }
     }
     $target_exists = file_exists($p);
 }
 # Check file exists and, if restricted access, that the user has access to the requested size.
 if (($target_exists && $access == 0 || $target_exists && $access == 1 && (image_size_restricted_access($size) || ($usesize = '' && $restricted_full_download))) && resource_download_allowed($ref, $usesize, $result[$n]['resource_type'])) {
     $used_resources[] = $ref;
     # when writing metadata, we take an extra security measure by copying the files to tmp
     $tmpfile = write_metadata($p, $ref, $id);
     // copies file
     if ($tmpfile !== false && file_exists($tmpfile)) {
         $p = $tmpfile;
         // file already in tmp, just rename it
     } else {
         if (!$replaced_file) {
             $copy = true;
             // copy the file from filestore rather than renaming
         }
     }
     # if the tmpfile is made, from here on we are working with that.
     # If using original filenames when downloading, copy the file to new location so the name is included.
				}
			else if (!$target_exists && $useoriginal == 'yes'
					&& resource_download_allowed($ref,'',$result[$n]['resource_type']))
				{
				// this size doesn't exist, so we'll try using the original instead
				$p=get_resource_path($ref,true,'',false,$result[$n]['file_extension'],-1,1,$use_watermark);
				$pextension = $result[$n]['file_extension'];
				$subbed_original_resources[] = $ref;
				$subbed_original = true;
				$target_exists = file_exists($p);
				}

			# Check file exists and, if restricted access, that the user has access to the requested size.
			if ((($target_exists && $access==0) ||
				($target_exists && $access==1 &&
					(image_size_restricted_access($size) || ($usesize='' && $restricted_full_download))) 
					) && resource_download_allowed($ref,$usesize,$result[$n]['resource_type']))
				{
				$used_resources[]=$ref;
				# when writing metadata, we take an extra security measure by copying the files to tmp
				$tmpfile=write_metadata($p,$ref,$id); // copies file
				if($tmpfile!==false && file_exists($tmpfile)){
					$p=$tmpfile; // file already in tmp, just rename it
				} else if (!$replaced_file) {
					$copy=true; // copy the file from filestore rather than renaming
				}

				# if the tmpfile is made, from here on we are working with that. 
				
				# If using original filenames when downloading, copy the file to new location so the name is included.
				$filename = '';