Beispiel #1
0
 public function index05Action()
 {
     if ($this->request->isPost()) {
         $upload = new \ZendVN\File\Upload();
         $fileName = $upload->getFileName();
         $upload->addValidator("Extension", true, array("png", "jpg"), "image")->addValidator("Size", false, array("min" => "100KB", "max" => "1MB"), "zip");
         foreach ($fileName as $key => $value) {
             if ($upload->isValid($key)) {
                 $prefix = $key == "image" ? "img_" : "file_";
                 $upload->uploadFile($key, FILES_PATH . $key, array("task" => "rename"), $prefix);
             } else {
                 $message = $upload->getMessages();
                 echo "<pre style='font-weight:bold'>";
                 print_r($message);
                 echo "</pre>";
             }
         }
     }
 }
 public function indexAction()
 {
     $error = array();
     //Tiêu đề
     $title = 'Thư viện ảnh';
     $this->headTitle($title)->setSeparator(" - ")->append("Hệ thống quản trị website");
     if ($this->getRequest()->isPost()) {
         //Upload
         $image = '';
         $upload = new \ZendVN\File\Upload();
         $fileName = $upload->getFileName();
         if (!empty($fileName)) {
             $upload->addValidator('Extension', true, array('png', 'jpg', 'gif'), 'file');
             $upload->addValidator('Size', false, array('min' => '20kb', 'max' => '5mb'), 'file');
             if (!$upload->isValid('file')) {
                 $messages = $upload->getMessages();
                 if (!empty($messages['fileExtensionFalse'])) {
                     $error[] = 'Định dạng file không hợp lệ!';
                 }
                 if (!empty($messages['fileSizeTooBig'])) {
                     $error[] = 'Kích thước file quá lớn!';
                 }
                 if (!empty($messages['fileSizeTooSmall'])) {
                     $error[] = 'Kích thước file quá nhỏ!';
                 }
             } else {
                 //upload ảnh mới
                 $file = $upload->uploadFile('file', UPLOAD_PATH . '/gallery/', array('task' => 'rename'), 'batdongsan_');
             }
         } else {
             $error[] = 'Bạn chưa chọn file upload';
         }
         if (empty($this->_arrPost['name'])) {
             $error[] = 'Bạn chưa nhập tên file';
         }
         if (empty($error)) {
             $data = array('user_id' => $this->identity()->id, 'images' => $file, 'date_time' => date('d/m/y h:i:s'), 'name' => $this->_arrPost['name']);
             $this->getTable()->saveItem($data, array('task' => 'save-file'));
             $this->flashMessenger()->addSuccessMessage('Dữ liệu đã được lưu thành công');
             $this->redirect()->toUrl('/admin/gallery/');
         }
     }
     return new ViewModel(array('title' => $title, 'arrParam' => $this->_arrParam, 'currentController' => $this->_currentController, 'error' => $error));
 }
 public function uploadAction()
 {
     $view = new ViewModel();
     $dataItems = null;
     $dataPaginator = null;
     $isXmlHttpRequest = false;
     //Disable Layout
     if ($this->getRequest()->isXmlHttpRequest() == true) {
         $isXmlHttpRequest = true;
         if ($this->getRequest()->isPost()) {
             $upload = new \ZendVN\File\Upload();
             $upload->addValidator('Extension', true, array('png', 'jpg'), 'image');
             $upload->addValidator('Size', false, array('min' => '1kb', 'max' => '500kb'), 'image');
             if ($upload->isValid('image')) {
                 //upload ảnh mới
                 $fileName = $upload->uploadFile('image', UPLOAD_PATH . '/email-template/', array('task' => 'rename'), 'batdongsan_');
                 //Thực hiện xóa ảnh cũ
                 $upload->removeFile(UPLOAD_PATH . '/email-template/' . $this->_arrPost['image_hidden']);
                 //update database
                 $arrParam = array('id' => $this->_arrPost['id'], 'images' => $fileName);
                 $this->getTable()->saveItem($arrParam, array('task' => 'edit'));
             } else {
                 $fileName = $this->_arrPost['image_hidden'];
                 $messages = $upload->getMessages();
                 if (!empty($messages['fileExtensionFalse'])) {
                     echo '<script>alert("Định dạng file không hợp lệ");</script>';
                 }
                 if (!empty($messages['fileSizeTooBig'])) {
                     echo '<script>alert("Kích thước file quá lớn");</script>';
                 }
                 if (!empty($messages['fileSizeTooSmall'])) {
                     echo '<script>alert("Kích thước file quá nhỏ");</script>';
                 }
             }
         }
     }
     $view->setVariables(array('isXmlHttpRequest' => $isXmlHttpRequest, 'fileName' => $fileName, 'arrParam' => $this->_arrParam, 'currentController' => $this->_currentController));
     $view->setTerminal(true);
     return $view;
 }
 public function uploadifyDbAction()
 {
     $result = array();
     if ($this->getRequest()->isPost()) {
         if (!empty($_FILES)) {
             //Upload File
             $upload = new \ZendVN\File\Upload();
             $fileName = $upload->getFileName();
             if (!empty($fileName)) {
                 $upload->addValidator('Extension', true, array('png', 'jpg'), 'Filedata');
                 $upload->addValidator('Size', false, array('min' => '10kb', 'max' => '500kb'), 'Filedata');
                 if (!$upload->isValid('Filedata')) {
                     $result['status'] = 'error';
                     $messages = $upload->getMessages();
                     if (!empty($messages['fileExtensionFalse'])) {
                         $result['messages']['upload'] = 'Định dạng file không hợp lệ';
                     }
                     if (!empty($messages['fileSizeTooBig'])) {
                         $result['messages']['upload'] = 'Kích thước file quá lớn';
                     }
                     if (!empty($messages['fileSizeTooSmall'])) {
                         $result['messages']['upload'] = 'Kích thước file quá nhỏ';
                     }
                 } else {
                     $filename = $upload->uploadFile('Filedata', UPLOAD_PATH . '/project/', array('task' => 'rename'), 'batdongsan_');
                     $item = $this->getTable()->getItem($this->_arrPost, array('task' => 'get-item'));
                     //Mảng hình ảnh lấy từ database
                     $Imgdata = \Zend\Json\Json::decode($item->images);
                     // mảng chưa file upload hiện tại
                     $newValue = array();
                     $newValue[] = $filename;
                     //Gộp 2 mảng
                     if (!empty($Imgdata)) {
                         $newValue = array_merge($newValue, $Imgdata);
                     }
                     //mảng chứa chuỗi json hình ảnh
                     $arrParam['id'] = $this->_arrPost['id'];
                     $arrParam['images'] = \Zend\Json\Json::encode($newValue);
                     //Cập nhật hình ảnh vào database
                     $this->getTable()->saveItem($arrParam, array('task' => 'edit'));
                     $result['status'] = 'success';
                     $result['messages']['upload'] = 'Thành công';
                 }
             }
         }
     }
     echo \Zend\Json\Json::encode($result);
     return $this->getResponse();
 }
 public function uploadBackgroundAction()
 {
     $view = new ViewModel();
     $dataItems = null;
     $dataPaginator = null;
     $isXmlHttpRequest = false;
     //Disable Layout
     if ($this->getRequest()->isXmlHttpRequest() == true) {
         $isXmlHttpRequest = true;
         if ($this->getRequest()->isPost()) {
             $upload = new \ZendVN\File\Upload();
             $upload->addValidator('Extension', true, array('png', 'jpg'), 'image');
             $upload->addValidator('Size', false, array('min' => '1kb', 'max' => '500kb'), 'image');
             if ($upload->isValid('image')) {
                 //upload ảnh mới
                 $fileName = $upload->uploadFile('image', UPLOAD_PATH . '/skin/', array('task' => 'rename'), 'batdongsan_');
                 //Thực hiện xóa ảnh cũ
                 $item = $this->getTable()->getItem($this->_arrParam, array('task' => 'get-item'));
                 $arrConfig = \Zend\Json\Json::decode($item->config_background);
                 foreach ($arrConfig->listBackground as $key => $item) {
                     $arrBackground['listBackground'][] = array('name' => $item->name, 'type' => $item->type);
                 }
                 $arrBackground['listBackground'][] = array('name' => $fileName, 'type' => 'system');
                 $arrBackground['curentBackground'] = array('background' => $arrConfig->curentBackground->background, 'style' => $arrConfig->curentBackground->style);
                 //Chống tấn công XSS
                 $purifier = new \HTMLPurifier_HTMLPurifier();
                 $arrParam = array('id' => 1, 'config_background' => \Zend\Json\Json::encode($arrBackground));
                 $this->getTable()->saveItem($arrParam, array('task' => 'edit'));
             } else {
                 $fileName = $this->_arrPost['image_hidden'];
                 $messages = $upload->getMessages();
                 if (!empty($messages['fileExtensionFalse'])) {
                     echo '<script>alert("Định dạng file không hợp lệ");</script>';
                 }
                 if (!empty($messages['fileSizeTooBig'])) {
                     echo '<script>alert("Kích thước file quá lớn");</script>';
                 }
                 if (!empty($messages['fileSizeTooSmall'])) {
                     echo '<script>alert("Kích thước file quá nhỏ");</script>';
                 }
             }
         }
     }
     $view->setVariables(array('isXmlHttpRequest' => $isXmlHttpRequest, 'fileName' => $fileName, 'arrParam' => $this->_arrParam, 'currentController' => $this->_currentController));
     $view->setTerminal(true);
     return $view;
 }