public function addInputFilter($user = null) { $inputFilter = new InputFilter\InputFilter(); $factory = new InputFactory(); $inputFilter->add($factory->createInput(array('name' => 'fileUpload', 'required' => true, 'validators' => array(array('name' => 'Zend\\Validator\\File\\Extension', 'break_chain_on_failure' => true, 'options' => $this->getAllowedExts(), 'messages' => array(\Zend\Validator\File\Extension::FALSE_EXTENSION => 'File upload phải là file png,jpeg,jpg,gif')))))); // File Path $uri = new \Home\Service\Uri(); $targetFolder = $uri->getSavePath($user); if (!file_exists($targetFolder)) { $oldmask = umask(0); mkdir($targetFolder, 0777, true); umask($oldmask); } $fileInput = new InputFilter\FileInput('fileUpload'); $fileInput->setRequired(true); $fileInput->getFilterChain()->attachByName('filerenameupload', array('target' => $targetFolder, 'use_upload_name' => true, 'overwrite' => true)); $inputFilter->add($fileInput); $this->setInputFilter($inputFilter); }
/** * @author DuongNq * @return \Zend\View\Model\JsonModel */ public function avatarAction() { $formData = $this->getRequest()->getPost()->toArray(); /** @var \User\Model\User $user */ $user = new User(); $userMapper = $this->getServiceLocator()->get('User\\Model\\UserMapper'); $user = $userMapper->get($this->user()->getIdentity()); $uri = new \Home\Service\Uri(); $datePath = DateBase::toFormat($user->getCreatedDateTime(), 'Ymd'); if (!file_exists(MEDIA_PATH . '/user/' . $datePath . '/')) { mkdir(MEDIA_PATH . '/user/' . $datePath . '/'); chmod(MEDIA_PATH . '/user/' . $datePath . '/', 0777); } if (!file_exists($uri->getSavePath($user))) { mkdir($uri->getSavePath($user)); chmod($uri->getSavePath($user), 0777); } $avatarFiles = scandir($uri->getSavePath($user)); for ($i = 2; $i < count($avatarFiles); $i++) { if ($user->getAvatar() != $avatarFiles[$i]) { unlink(MEDIA_PATH . '/user/avatar/' . $avatarFiles[$i]); } } $file = $this->params()->fromFiles(); $form = new \User\Form\ProfileFile('fileUpload'); $form->addInputFilter($user); $tempFile = null; foreach ($file as $key => $fileInfo) { $fileInfo['name'] = time() . rand(1, 1000) . '.' . pathinfo($fileInfo['name'], PATHINFO_EXTENSION); $formData['fileUpload'] = $fileInfo; if (getimagesize($fileInfo['tmp_name'])[0] != 119 && getimagesize($fileInfo['tmp_name'])[0] != 119) { if (!$this->smart_resize_image($fileInfo['tmp_name'], null, 119, 119)) { $json = new JsonModel(); $json->setVariables(['code' => 1, 'messages' => 'Ảnh bị lỗi, mời bạn thử ảnh khác']); return $json; } } $form->setData($formData); if ($form->isValid()) { $data = $form->getData(); $user->setAvatar($data['fileUpload']['name']); $userMapper->save($user); } else { // Form not valid, but file uploads might be valid... // Get the temporary file information to show the user in the view $fileErrors = $form->get('fileUpload')->getMessages(); $json = new JsonModel($fileErrors); return $json; if (empty($fileErrors)) { $tempFile = $form->get('fileUpload')->getValue(); } } } $variables = array('code' => 0, 'path' => $uri->getViewPath($user)); $json = new JsonModel($variables); return $json; }