Beispiel #1
0
 /**
  * @param \MfccAdminModule\Form\Element\File $element
  * @param \MfccAdminModule\Entity\User|null $user
  * @param array $option
  * @return File
  */
 public function upload($element, \MfccAdminModule\Entity\User $user = null, array $option = [])
 {
     $_file = $element->getValue();
     if ($_file['error'] != 0) {
         return null;
     }
     $fileName = $_file['name'];
     $mimetype = $_file['type'];
     $hash = md5(microtime(true) . $fileName);
     $savePath = substr($hash, 0, 1) . '/' . substr($hash, 1, 1) . '/';
     $file = new File();
     if ($user) {
         $file->setInsertedBy($user->getId());
     }
     if (isset($option['fileName'])) {
         $file->setName($option['fileName']);
     } else {
         $file->setName($fileName);
     }
     $file->setMimetype($mimetype);
     $file->setSize($_file['size']);
     $file->setActive($this->params['default_is_active']);
     $file->setSavePath($savePath . $hash);
     if (isset($option['keywords'])) {
         $this->addKeywordsToFile($option['keywords']);
     }
     try {
         $this->getFilesystem()->writeStream($savePath . $hash, fopen($_file['tmp_name'], 'r+'));
         $element->setFileObject($file);
         $this->getEntityManager()->persist($file);
     } catch (\Exception $e) {
         throw new Exception\RuntimeException('File cannot be saved.', 0, $e);
     }
     return $file;
 }