Skip to content

Instantly share code, notes, and snippets.

@jkersu
Created June 4, 2020 11:56
Show Gist options
  • Save jkersu/6e81596cf177151802c006899c00dd54 to your computer and use it in GitHub Desktop.
Save jkersu/6e81596cf177151802c006899c00dd54 to your computer and use it in GitHub Desktop.
SilverStripe 4 - Upload file from uploaded file from frontend form
if (isset($data['FileID']) && $data['FileID'] && $data['FileID']['error'] == 0) {
$attachment = $data['FileID'];
try {
$upload = Upload::create();
$file = File::create();
$file->Title = $attachment['name'];
$folder = Folder::find_or_make('Uploads/FolderName/' . Security::getCurrentUser()->ID);
$upload->loadIntoFile($attachment, $file, $folder->getFilename());
$fileID = $file->write();
$app->AttachmentID = $fileID;
$app->FileUploadDate = DBDatetime::now()->Format('y-MM-dd');
} catch (ValidationException $e) {
$form->sessionMessage('There was an issue with uploading the file. Please try again.', 'bad');
return;
} catch (\Exception $e) {
$form->sessionMessage('There was an issue with uploading the file. Please try again.', 'bad');
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment