Generate File from frontend FileField

Silverstripe Version: 4.2

Question: How to store a File

When I use $file->setFromString it creates a FileObject but:

  1. Filename in DB is null
  2. When I Upload a second File with the same Name it creates a new File Object and it’s naming it v2,v3 ect. but it points to the SAME F*CKING FILE there is only one File on Servers Disk.

The Documentation here:
is outdated. It says “The conflict resolution to use can be passed into the third parameter of the above methods” third parameter is a hash.

	public function submit($data, $form){
		if ( !$data["Dokument"]["error"] > 0 ) {
			$image = $data['Dokument'];
			$content = file_get_contents($image['tmp_name']);
			$file = File::create();
			$file->setFromString($content, $image['name']);
			$file->ParentID = 95263;

The third parameter (hash) is the hash of the content. As long as it’s the same for 2 files, they can be stored at the same place on the filesystem. If you leave it empty, the AssetStore computes it on the fly from the content you’re passing into the method. Since you’re giving it the same content again and again, it links it to the same file on the filesystem.