The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

When the user uploads their various image files and submits the form the createAction() funciton of app/controllers/MemberimageController.php will be called. We now need to modify this function to allow it to process the array of files that will be uploaded. Replace the createAction() function that was generated by the scaffold with the one below.

public function createAction()
{
	if (!$this->request->isPost()) {
		$this->dispatcher->forward(['controller' => "Memberimage",'action' => 'new']);
		return;
	}

	$mid = $this->request->getPost("memberid");
	if ($this->request->hasFiles() == true) {
		$i=0;
		foreach ($this->request->getUploadedFiles() as $file) {
			$image = new Memberimage();
			$image->setMemberid($mid);
			$image->setImagefile(base64_encode(file_get_contents($file->getTempName())));
			$image->setDescription($this->request->getPost("description")[$i++]);
			if (!$image->save()) {
				foreach ($image->getMessages() as $message) {
					$this->flash->error($message);
				}
			}				
		}
	}
	$this->dispatcher->forward(['controller' => "member",'action' => 'search']);
	return;
}

The initial if statement was generated by the scaffolder and merely checks to ensure that this function was arrived at via POST as opposed to GET. As this function's only purpose is to process the results of a POST form Users should only arrive here via GET. 

The following if statement checks to see if there are files attached to the POST. If there are a foreach loop will loop through each attached file, base64 encode it and save it to the database along with it's description. As with other scaffolded createAction() functions, if the record does not save correctly associated error messages will be output.

In the next post I will describe how to display groups of images which have been uploaded in this way.