public insertUploads ( string $name, array $metadata = [], array $options = [] ) : mixed | ||
$name | string | name of the uploaded file to store. This should correspond to the file field's name attribute in the HTML form. |
$metadata | array | other metadata fields to include in the file document. |
$options | array | list of options in format: optionName => optionValue |
return | mixed | the "_id" of the saved file document. This will be a generated [[\MongoId]] unless an "_id" was explicitly specified in the metadata. |
public function insertUploads($name, $metadata = [], $options = [])
{
$uploadedFile = UploadedFile::getInstanceByName($name);
if ($uploadedFile === null) {
throw new Exception("Uploaded file '{$name}' does not exist.");
}
$options['filename'] = $uploadedFile->name;
$options['document'] = $metadata;
$document = $this->createUpload($options)->addFile($uploadedFile->tempName)->complete();
return $document['_id'];
}