Created
June 4, 2020 11:56
-
-
Save jkersu/6e81596cf177151802c006899c00dd54 to your computer and use it in GitHub Desktop.
SilverStripe 4 - Upload file from uploaded file from frontend form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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