public function uploadAction() { $id_slab = (int) $this->getEvent()->getRouteMatch()->getParam('id_slab'); $aReturn = array(); $aDataInsert = array(); $sDestination = ROOT_DIR . '/upload/slab/' . $id_slab . '/'; (int) ($id_user = $this->getAuthService()->getStorage()->read()->id); $aDataInsert['id_slab'] = $id_slab; $aDataInsert['cr_date'] = new \Zend\Db\Sql\Expression("getdate()"); $aDataInsert['cr_user'] = $id_user; if (!is_dir($sDestination)) { if (!@mkdir($sDestination, 0777, true)) { throw new \Exception("Unable to create destination: " . $sDestination); } } $size = new Size(array('min' => 10, "max" => 10000000)); //minimum bytes filesize $count = new Count(array("min" => 0, "max" => 1)); $extension = new Extension(array("extension" => array("jpg", "png", "swf"))); $adapter = new \Zend\File\Transfer\Adapter\Http(); $file = $adapter->getFileInfo(); $adapter->setValidators(array($size, $count, $extension), $file['file']['name']); $adapter->setDestination($sDestination); if ($adapter->isValid()) { if ($adapter->isUploaded()) { if ($adapter->receive()) { $file = $adapter->getFileInfo(); $aDataInsert['photo'] = $file['file']['name']; $aDataInsert['name'] = $file['file']['name']; $this->getContainerDrawingTable()->save($aDataInsert); $aReturn['state'] = true; } } else { $aReturn['state'] = false; } } else { $dataError = $adapter->getMessages(); $error = array(); foreach ($dataError as $key => $row) { $error[] = $row; } $aReturn['state'] = false; } return new JsonModel($aReturn); }
/** * See if file has been received and uploaded. * * @param Http $adapter * @param string $fileName * * @return array */ private function validateUploadedFile(Http $adapter, $fileName) { $uploadStatus = []; $adapter->receive($fileName); if (!$adapter->isReceived($fileName) && $adapter->isUploaded($fileName)) { $uploadStatus['errorFiles'][] = $fileName . ' was not uploaded'; } else { $uploadStatus['successFiles'][] = $fileName . ' was successfully uploaded'; } return $uploadStatus; }
public function testFileIsNotUploaded() { $this->assertFalse($this->adapter->isUploaded('imageNotUpload')); }
/** * @param int $userId - used to create a folder for the current user and rename the image * * @return void */ private function uploadImage($userId = 0) { $userId = (int) $userId; $messages = []; $dir = 'public/userfiles/images/user-' . $userId . '/'; $adapter = new Http(); $size = new Size(['min' => '10kB', 'max' => '5MB', 'useByteString' => true]); $extension = new Extension(['jpg', 'gif', 'png', 'jpeg', 'bmp', 'webp', 'svg'], true); if (!is_dir($dir)) { mkdir($dir, 0750, true); } foreach ($adapter->getFileInfo() as $file) { $ext = pathinfo($file['name'], PATHINFO_EXTENSION); $newName = $userId . '.' . $ext; $adapter->setValidators([$size, new IsImage(), $extension]); $adapter->addFilter('File\\Rename', ['target' => $dir . $newName, 'overwrite' => true]); $adapter->receive($file['name']); if (!$adapter->isReceived($file['name']) && $adapter->isUploaded($file['name'])) { $messages[] = $file['name'] . ' was not uploaded'; } } $this->setLayoutMessages($messages, 'info'); }