当前位置: 首页>>代码示例>>PHP>>正文


PHP Upload::loadIntoFile方法代码示例

本文整理汇总了PHP中Upload::loadIntoFile方法的典型用法代码示例。如果您正苦于以下问题:PHP Upload::loadIntoFile方法的具体用法?PHP Upload::loadIntoFile怎么用?PHP Upload::loadIntoFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Upload的用法示例。


在下文中一共展示了Upload::loadIntoFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: saveInto

 public function saveInto(DataObject $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $hasOnes = $record->has_one($this->name);
         // try to create a file matching the relation
         $file = is_string($hasOnes) ? Object::create($hasOnes) : new File();
     } else {
         $file = new File();
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
     if ($this->upload->isError()) {
         return false;
     }
     $file = $this->upload->getFile();
     if ($this->relationAutoSetting) {
         if (!$hasOnes) {
             return false;
         }
         // save to record
         $record->{$this->name . 'ID'} = $file->ID;
     }
 }
开发者ID:SustainableCoastlines,项目名称:loveyourwater,代码行数:26,代码来源:FileField.php

示例2: saveInto

 public function saveInto(DataObjectInterface $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     $fileClass = File::get_class_for_file_extension(pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION));
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $hasOnes = $record->has_one($this->name);
         // try to create a file matching the relation
         $file = is_string($hasOnes) ? Object::create($hasOnes) : new $fileClass();
     } else {
         $file = new $fileClass();
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
     if ($this->upload->isError()) {
         return false;
     }
     $file = $this->upload->getFile();
     if ($this->relationAutoSetting) {
         if (!$hasOnes) {
             return false;
         }
         // save to record
         $record->{$this->name . 'ID'} = $file->ID;
     }
     return $this;
 }
开发者ID:prostart,项目名称:cobblestonepath,代码行数:28,代码来源:FileField.php

示例3: handleswfupload

 public function handleswfupload()
 {
     if (isset($_FILES["swfupload_file"]) && is_uploaded_file($_FILES["swfupload_file"]["tmp_name"])) {
         $file = new File();
         $u = new Upload();
         $u->loadIntoFile($_FILES['swfupload_file'], $file, "Resumes");
         $file->write();
         echo $file->ID;
     } else {
         echo ' ';
         // return something or SWFUpload won't fire uploadSuccess
     }
 }
开发者ID:racontemoi,项目名称:shibuichi,代码行数:13,代码来源:SWFUploadControls.php

示例4: index

	public function index(SS_HTTPRequest $r) {
		if(isset($_FILES["Filedata"]) && is_uploaded_file($_FILES["Filedata"]["tmp_name"])) {
			$upload_folder = urldecode($r->requestVar('uploadFolder'));
			if(isset($_REQUEST['FolderID'])) {
				if($folder = DataObject::get_by_id("Folder", Convert::raw2sql($_REQUEST['FolderID']))) {
					$upload_folder = UploadifyField::relative_asset_dir($folder->Filename);
				}
			}
			$ext = strtolower(end(explode('.', $_FILES['Filedata']['name'])));
			$class = in_array($ext, UploadifyField::$image_extensions) ? $r->requestVar('imageClass') : $r->requestVar('fileClass');
			$file = new $class();
			$u = new Upload();
			$u->loadIntoFile($_FILES['Filedata'], $file, $upload_folder);
			$file->write();
			echo $file->ID;
		} 
		else {
			echo ' '; // return something or SWFUpload won't fire uploadSuccess
		}	
	}
开发者ID:rmpel,项目名称:Uploadify,代码行数:20,代码来源:UploadifyUploader.php

示例5: doUpload

 function doUpload($data, $form)
 {
     if (isset($data['UploadedMedia']['tmp_name'])) {
         if (!empty($data['UploadedMedia']['name'])) {
             // create new single file array from file uploads array
             $file = array();
             $file['name'] = $data['UploadedMedia']['name'];
             $file['type'] = $data['UploadedMedia']['type'];
             $file['tmp_name'] = $data['UploadedMedia']['tmp_name'];
             $file['error'] = $data['UploadedMedia']['error'];
             $file['size'] = $data['UploadedMedia']['size'];
             // create & write uploaded file in DB
             try {
                 $newfile = new File();
                 $upload = new Upload();
                 // get folder from form upload field
                 $folder = $form->Fields()->fieldByName('UploadedMedia')->getFolderName();
                 $upload->loadIntoFile($file, $newfile, $folder);
                 $fileObj = $upload->getFile();
                 $EventID = Session::get('UploadMedia.PresentationID');
                 if ($EventID) {
                     $Event = VideoPresentation::get()->byID($EventID);
                 }
                 if (isset($Event)) {
                     $Event->UploadedMediaID = $fileObj->ID;
                     $Event->MediaType = 'File';
                     $Event->write();
                     Session::set('UploadMedia.Success', TRUE);
                     Session::set('UploadMedia.FileName', $fileObj->Name);
                     Session::set('UploadMedia.Type', 'File');
                     Controller::curr()->redirect($form->controller()->link() . 'Success');
                 }
             } catch (ValidationException $e) {
                 $form->sessionMessage('Extension not allowed...', 'bad');
                 return $this->controller()->redirectBack();
             }
         }
     }
 }
开发者ID:Thingee,项目名称:openstack-org,代码行数:39,代码来源:PresentationMediaUploadForm.php

示例6: saveInto

 public function saveInto(DataObjectInterface $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     $fileClass = File::get_class_for_file_extension(File::get_file_extension($_FILES[$this->name]['name'], PATHINFO_EXTENSION));
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $objectClass = $record->hasOne($this->name);
         if ($objectClass === 'File' || empty($objectClass)) {
             // Create object of the appropriate file class
             $file = Object::create($fileClass);
         } else {
             // try to create a file matching the relation
             $file = Object::create($objectClass);
         }
     } else {
         if ($record instanceof File) {
             $file = $record;
         } else {
             $file = Object::create($fileClass);
         }
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->getFolderName());
     if ($this->upload->isError()) {
         return false;
     }
     if ($this->relationAutoSetting) {
         if (!$objectClass) {
             return false;
         }
         $file = $this->upload->getFile();
         $record->{$this->name . 'ID'} = $file->ID;
     }
     return $this;
 }
开发者ID:ivoba,项目名称:silverstripe-framework,代码行数:36,代码来源:FileField.php

示例7: imageupload

 public function imageupload()
 {
     if (!Member::currentUserID()) {
         $return = array('error' => 1, 'text' => "Cannot upload there");
         return Convert::raw2json($return);
     }
     if (isset($_FILES['NewImage']) && ($tempfile = $_FILES['NewImage'])) {
         // validate //
         $allowed = array('jpg', 'jpeg', 'gif', 'png', 'ico');
         $nameBits = explode('.', $tempfile['name']);
         $ext = end($nameBits);
         if (!in_array(strtolower($ext), $allowed)) {
             $return = array('error' => 1, 'text' => "Your image must be in jpg, gif or png format");
             return Convert::raw2json($return);
         }
         $maxsize = $_POST['MAX_FILE_SIZE'];
         if ($tempfile['size'] > $maxsize) {
             $size = number_format($maxsize / 1024 / 1024, 2) . 'MB';
             $return = array('error' => 1, 'text' => "Your image must be smaller than {$size}");
             return Convert::raw2json($return);
         }
         // upload //
         $upload = new Upload();
         $file = new Image();
         $upload->loadIntoFile($tempfile, $file);
         if ($upload->isError()) {
             return false;
         }
         $file = $upload->getFile();
         $return = array('link' => $file->Link());
         return Convert::raw2json($return);
     } else {
         // no file to upload
         return false;
     }
 }
开发者ID:nyeholt,项目名称:silverstripe-simplewiki,代码行数:36,代码来源:WikiPage.php

示例8: process

 /**
  * Process the form that is submitted through the site
  * 
  * @param Array Data
  * @param Form Form 
  * @return Redirection
  */
 public function process($data, $form)
 {
     Session::set("FormInfo.{$form->FormName()}.data", $data);
     Session::clear("FormInfo.{$form->FormName()}.errors");
     foreach ($this->Fields() as $field) {
         $messages[$field->Name] = $field->getErrorMessage()->HTML();
         if ($field->Required && $field->CustomRules()->Count() == 0) {
             if (!isset($data[$field->Name]) || !$data[$field->Name] || !$field->getFormField()->validate($this->validator)) {
                 $form->addErrorMessage($field->Name, $field->getErrorMessage()->HTML(), 'bad');
             }
         }
     }
     if (Session::get("FormInfo.{$form->FormName()}.errors")) {
         Controller::curr()->redirectBack();
         return;
     }
     $submittedForm = Object::create('SubmittedForm');
     $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
     $submittedForm->ParentID = $this->ID;
     // if saving is not disabled save now to generate the ID
     if (!$this->DisableSaveSubmissions) {
         $submittedForm->write();
     }
     $values = array();
     $attachments = array();
     $submittedFields = new ArrayList();
     foreach ($this->Fields() as $field) {
         if (!$field->showInReports()) {
             continue;
         }
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->getField('Title');
         // save the value from the data
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $file->ShowInSearch = 0;
                     try {
                         $upload->loadIntoFile($_FILES[$field->Name], $file);
                     } catch (ValidationException $e) {
                         $validationResult = $e->getResult();
                         $form->addErrorMessage($field->Name, $validationResult->message(), 'bad');
                         Controller::curr()->redirectBack();
                         return;
                     }
                     // write file to form field
                     $submittedField->UploadedFileID = $file->ID;
                     // attach a file only if lower than 1MB
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                 }
             }
         }
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     // email users on submit.
     if ($this->EmailRecipients()) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         if ($attachments) {
             foreach ($attachments as $file) {
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, HTTP::get_mime_type($file->Filename));
                 }
             }
         }
         foreach ($this->EmailRecipients() as $recipient) {
             $email->populateTemplate($recipient);
             $email->populateTemplate($emailData);
             $email->setFrom($recipient->EmailFrom);
             $email->setBody($recipient->EmailBody);
             $email->setSubject($recipient->EmailSubject);
             $email->setTo($recipient->EmailAddress);
             // check to see if they are a dynamic sender. eg based on a email field a user selected
             if ($recipient->SendEmailFromField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
//.........这里部分代码省略.........
开发者ID:nzjoel,项目名称:silverstripe-userforms,代码行数:101,代码来源:UserDefinedForm.php

示例9: process

 /**
  * Process the form that is submitted through the site
  * 
  * @param Array Data
  * @param Form Form 
  * @return Redirection
  */
 function process($data, $form)
 {
     $submittedForm = Object::create('SubmittedForm');
     $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
     $submittedForm->ParentID = $this->ID;
     // if saving is not disabled save now to generate the ID
     if (!$this->DisableSaveSubmissions) {
         $submittedForm->write();
     }
     $values = array();
     $attachments = array();
     $submittedFields = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         if (!$field->showInReports()) {
             continue;
         }
         // create a new submitted form field.
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->Title;
         // save the value from the data
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $upload->loadIntoFile($_FILES[$field->Name], $file);
                     // write file to form field
                     $submittedField->UploadedFileID = $file->ID;
                     // attach a file only if lower than 1MB
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                 }
             }
         }
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     // email users on submit.
     if ($this->EmailRecipients()) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         if ($attachments) {
             foreach ($attachments as $file) {
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, $file->getFileType());
                 }
             }
         }
         foreach ($this->EmailRecipients() as $recipient) {
             $email->populateTemplate($recipient);
             $email->populateTemplate($emailData);
             $email->setFrom($recipient->EmailFrom);
             $email->setBody($recipient->EmailBody);
             $email->setSubject($recipient->EmailSubject);
             $email->setTo($recipient->EmailAddress);
             // check to see if they are a dynamic sender. eg based on a email field a user selected
             if ($recipient->SendEmailFromField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
                 if ($submittedFormField) {
                     $email->setFrom($submittedFormField->Value);
                 }
             }
             // check to see if they are a dynamic reciever eg based on a dropdown field a user selected
             if ($recipient->SendEmailToField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);
                 if ($submittedFormField) {
                     $email->setTo($submittedFormField->Value);
                 }
             }
             if ($recipient->SendPlain) {
                 $body = strip_tags($recipient->EmailBody) . "\n ";
                 if (isset($emailData['Fields']) && !$recipient->HideFormData) {
                     foreach ($emailData['Fields'] as $Field) {
                         $body .= $Field->Title . ' - ' . $Field->Value . ' \\n';
                     }
                 }
                 $email->setBody($body);
                 $email->sendPlain();
             } else {
//.........这里部分代码省略.........
开发者ID:SustainableCoastlines,项目名称:loveyourwater,代码行数:101,代码来源:UserDefinedForm.php

示例10: loadUploaded

 /**
  * Save an file passed from a form post into this object.
  * DEPRECATED Please instanciate an Upload-object instead and pass the file
  * via {Upload->loadIntoFile()}.
  * 
  * @param $tmpFile array Indexed array that PHP generated for every file it uploads.
  * @return Boolean|string Either success or error-message.
  */
 function loadUploaded($tmpFile)
 {
     user_error('File::loadUploaded is deprecated, please use the Upload class directly.', E_USER_NOTICE);
     $upload = new Upload();
     $upload->loadIntoFile($tmpFile, $this);
     return $upload->isError();
 }
开发者ID:racontemoi,项目名称:shibuichi,代码行数:15,代码来源:File.php

示例11: handleswfupload

 function handleswfupload()
 {
     set_time_limit(1200);
     // 20 minutes
     $data = $_POST;
     $owner = DataObject::get_by_id($this->urlParams['Class'], $this->urlParams['ID']);
     $fieldName = $this->urlParams['Field'] . 'ID';
     // TODO We need to replace this with a way to get the type of a field
     $imageClass = $owner->has_one($this->urlParams['Field']);
     // If we can't find the relationship, assume its an Image.
     if (!$imageClass) {
         if (!is_subclass_of($imageClass, 'Image')) {
             $imageClass = 'Image';
         }
     }
     // Assuming its a decendant of File
     $image = new $imageClass();
     if (class_exists("Upload")) {
         $u = new Upload();
         $u->loadIntoFile($_FILES['swfupload_file'], $image);
     } else {
         $image->loadUploaded($_FILES['swfupload_file']);
     }
     $owner->{$fieldName} = $image->ID;
     // store the owner id with the uploaded image
     $image->write();
     $owner->write();
     echo $owner->ID;
 }
开发者ID:racontemoi,项目名称:shibuichi,代码行数:29,代码来源:SWFUploadFileIFrameField.php

示例12: uploadSpeakerPic

 /**
  * @param ISummit $summit
  * @param $speaker_id
  * @param $tmp_file
  * @return BetterImage
  */
 public function uploadSpeakerPic(ISummit $summit, $speaker_id, $tmp_file)
 {
     $speaker_repository = $this->speaker_repository;
     return $this->tx_service->transaction(function () use($summit, $speaker_id, $tmp_file, $speaker_repository) {
         $speaker_id = intval($speaker_id);
         $speaker = $speaker_repository->getById($speaker_id);
         if (is_null($speaker)) {
             throw new NotFoundEntityException('PresentationSpeaker');
         }
         $image = new BetterImage();
         $upload = new Upload();
         $validator = new Upload_Validator();
         $validator->setAllowedExtensions(array('png', 'jpg', 'jpeg', 'gif'));
         $validator->setAllowedMaxFileSize(800 * 1024);
         // 300Kb
         $upload->setValidator($validator);
         if (!$upload->loadIntoFile($tmp_file, $image, 'profile-images')) {
             throw new EntityValidationException($upload->getErrors());
         }
         $image->write();
         return $image;
     });
 }
开发者ID:hogepodge,项目名称:openstack-org,代码行数:29,代码来源:SummitService.php

示例13: process

 /**
  * Process the form that is submitted through the site
  * 
  * @param Array Data
  * @param Form Form 
  * @return Redirection
  */
 function process($data, $form)
 {
     // submitted form object
     $submittedForm = new SubmittedForm();
     $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
     $submittedForm->ParentID = $this->ID;
     $submittedForm->Recipient = $this->EmailTo;
     if (!$this->DisableSaveSubmissions) {
         $submittedForm->write();
     }
     // email values
     $values = array();
     $recipientAddresses = array();
     $sendCopy = false;
     $attachments = array();
     $submittedFields = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         // don't show fields that shouldn't be shown
         if (!$field->showInReports()) {
             continue;
         }
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->Title;
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $upload->loadIntoFile($_FILES[$field->Name], $file);
                     // write file to form field
                     $submittedField->UploadedFileID = $file->ID;
                     // Attach the file if its less than 1MB, provide a link if its over.
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                 }
             }
         }
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     // email users on submit. All have their own custom options.
     if ($this->EmailRecipients()) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         if ($attachments) {
             foreach ($attachments as $file) {
                 // bug with double decorated fields, valid ones should have an ID.
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, $file->getFileType());
                 }
             }
         }
         foreach ($this->EmailRecipients() as $recipient) {
             $email->populateTemplate($recipient);
             $email->populateTemplate($emailData);
             $email->setFrom($recipient->EmailFrom);
             $email->setBody($recipient->EmailBody);
             $email->setSubject($recipient->EmailSubject);
             $email->setTo($recipient->EmailAddress);
             // check to see if they are a dynamic sender. eg based on a email field a user selected
             if ($recipient->SendEmailFromField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
                 if ($submittedFormField) {
                     $email->setFrom($submittedFormField->Value);
                 }
             }
             // check to see if they are a dynamic reciever eg based on a dropdown field a user selected
             if ($recipient->SendEmailToField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);
                 if ($submittedFormField) {
                     $email->setTo($submittedFormField->Value);
                 }
             }
             if ($recipient->SendPlain) {
                 $body = strip_tags($recipient->EmailBody) . "\n ";
                 if (isset($emailData['Fields']) && !$recipient->HideFormData) {
                     foreach ($emailData['Fields'] as $Field) {
                         $body .= $Field->Title . ' - ' . $Field->Value . ' \\n';
                     }
//.........这里部分代码省略.........
开发者ID:nicmart,项目名称:comperio-site,代码行数:101,代码来源:UserDefinedForm.php

示例14: handleswfupload

 public function handleswfupload()
 {
     if (isset($_FILES['swfupload_file']) && !empty($_FILES['swfupload_file'])) {
         $do_class = $_POST['dataObjectClassName'];
         $file_class = $_POST['fileClassName'];
         $obj = new $do_class();
         $idxfield = $_POST['fileFieldName'] . "ID";
         $file = new $file_class();
         $album = DataObject::get_by_id("ImageGalleryAlbum", $_POST['AlbumID']);
         $dest = substr_replace(str_replace('assets/', '', $album->Folder()->Filename), "", -1);
         if (class_exists("Upload")) {
             $u = new Upload();
             $u->loadIntoFile($_FILES['swfupload_file'], $file, $dest);
         } else {
             $file->loadUploaded($_FILES['swfupload_file'], $dest);
         }
         $file->setField("ParentID", $album->FolderID);
         $file->write();
         $obj->{$idxfield} = $file->ID;
         $obj->AlbumID = $album->ID;
         $ownerID = $_POST['parentIDName'];
         $obj->{$ownerID} = $_POST['controllerID'];
         $obj->write();
         echo $obj->ID;
     } else {
         echo ' ';
     }
 }
开发者ID:racontemoi,项目名称:shibuichi,代码行数:28,代码来源:ImageGalleryManager.php

示例15: process

 /**
  * Process the form that is submitted through the site
  * 
  * @param array $data
  * @param Form $form
  *
  * @return Redirection
  */
 public function process($data, $form)
 {
     Session::set("FormInfo.{$form->FormName()}.data", $data);
     Session::clear("FormInfo.{$form->FormName()}.errors");
     foreach ($this->Fields() as $field) {
         $messages[$field->Name] = $field->getErrorMessage()->HTML();
         $formField = $field->getFormField();
         if ($field->Required && $field->CustomRules()->Count() == 0) {
             if (isset($data[$field->Name])) {
                 $formField->setValue($data[$field->Name]);
             }
             if (!isset($data[$field->Name]) || !$data[$field->Name] || !$formField->validate($form->getValidator())) {
                 $form->addErrorMessage($field->Name, $field->getErrorMessage(), 'bad');
             }
         }
     }
     if (Session::get("FormInfo.{$form->FormName()}.errors")) {
         Controller::curr()->redirectBack();
         return;
     }
     $submittedForm = Object::create('SubmittedForm');
     $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
     $submittedForm->ParentID = $this->ID;
     // if saving is not disabled save now to generate the ID
     if (!$this->DisableSaveSubmissions) {
         $submittedForm->write();
     }
     $values = array();
     $attachments = array();
     $submittedFields = new ArrayList();
     foreach ($this->Fields() as $field) {
         if (!$field->showInReports()) {
             continue;
         }
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->getField('Title');
         // save the value from the data
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     $foldername = $field->getFormField()->getFolderName();
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $file->ShowInSearch = 0;
                     try {
                         $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername);
                     } catch (ValidationException $e) {
                         $validationResult = $e->getResult();
                         $form->addErrorMessage($field->Name, $validationResult->message(), 'bad');
                         Controller::curr()->redirectBack();
                         return;
                     }
                     // write file to form field
                     $submittedField->UploadedFileID = $file->ID;
                     // attach a file only if lower than 1MB
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                 }
             }
         }
         $submittedField->extend('onPopulationFromField', $field);
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     $this->extend('updateEmailData', $emailData, $attachments);
     // email users on submit.
     if ($recipients = $this->FilteredEmailRecipients($data, $form)) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $mergeFields = $this->getMergeFieldsMap($emailData['Fields']);
         if ($attachments) {
             foreach ($attachments as $file) {
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, HTTP::get_mime_type($file->Filename));
                 }
             }
         }
         foreach ($recipients as $recipient) {
             $parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields);
//.........这里部分代码省略.........
开发者ID:sekjal,项目名称:silverstripe-userforms,代码行数:101,代码来源:UserDefinedForm.php


注:本文中的Upload::loadIntoFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。