示例#1
0
 public static function httpUpload($field, $options, &$error)
 {
     $origin_file_name = $_FILES[$field]['name'];
     $file_name = $options['use_filename'] ? $options['use_filename'] : $origin_file_name;
     $file_path = $options['bucket'] . '/' . ($options['use_filename'] ? $options['use_filename'] : $origin_file_name);
     $path = dirname($file_path);
     $real_file_path = MEDIA_DIR . '/' . $file_path;
     if (!self::makeFileStorageDir($real_file_path)) {
         Log::getInstance()->error("Can not make storage directory {$real_file_path}");
         return false;
     }
     if (!$options['overwrite'] && file_exists($real_file_path)) {
         return $file_path;
     }
     $uploader = new Uploader(dirname($real_file_path));
     $uploader->setFieldUpload($field);
     $uploader->setOverwriteIfExists($options['overwrite']);
     $uploader->setNameAfterUploaded(basename($file_name));
     $uploader->setFilterType('.jpg, .jpeg, .png, .bmp, .jpe');
     if ($uploader->upload()) {
         $data = $uploader->getData();
         $file_path = $path . '/' . $data['file_name'];
         return 'media/' . $file_path;
     } else {
         $error = $uploader->getError();
         Log::getInstance()->warning("Can not upload file", $error);
     }
     return false;
 }
示例#2
0
 public function executeUpload()
 {
     $upload_max_filesize = str_replace('M', '', ini_get('upload_max_filesize'));
     $res = new \AjaxResponse();
     $upload_dir = PUBLIC_DIR . '/media/editor_upload/';
     Folder::create($upload_dir, 0777);
     $error = array();
     $fileUploader = new Uploader($upload_dir, 'file_upload');
     $fileUploader->setMaximumFileSize($upload_max_filesize);
     $fileUploader->setFilterType('.jpg, .jpeg, .png, .bmp, .gif');
     $fileUploader->setIsEncryptFileName(true);
     if ($fileUploader->upload('file_upload')) {
         $data = $fileUploader->getData();
         $file_path = '/media/editor_upload/' . $data['file_name'];
         $file_url = rtrim($this->document()->getBaseUrl(), '/') . preg_replace('/(\\/+)/', '/', '/../' . $file_path);
         $image = ['file_name' => $data['file_name'], 'url' => $file_url, 'path' => $file_path];
         $res->type = \AjaxResponse::SUCCESS;
         $res->image = $image;
         return $this->renderText($res->toString());
     } else {
         $error['upload'] = $fileUploader->getError();
     }
     $res->type = \AjaxResponse::ERROR;
     $res->error = $error;
     return $this->renderText($res->toString());
 }
示例#3
0
 public function executeUpload()
 {
     $upload_max_filesize = str_replace('M', '', ini_get('upload_max_filesize'));
     $res = new \AjaxResponse();
     $postId = $this->request()->post('post_id');
     $upload_dir = PUBLIC_DIR . '/media/post_img/';
     Folder::create($upload_dir, 0777);
     $error = array();
     $fileUploader = new Uploader($upload_dir, 'file_upload');
     $fileUploader->setMaximumFileSize($upload_max_filesize);
     $fileUploader->setFilterType('.jpg, .jpeg, .png, .bmp, .gif');
     $fileUploader->setIsEncryptFileName(true);
     if ($fileUploader->upload('upload_images')) {
         $data = $fileUploader->getData();
         $filePath = '/media/post_img/' . $data['file_name'];
         $fileUrl = $this->document()->getBaseUrl() . '/../' . $filePath;
         $otherImages = \PostPeer::getPostImg($postId);
         $ordering = sizeof($otherImages);
         foreach ($otherImages as $image) {
             if ($image->getOrdering() > $ordering) {
                 $ordering = $image->getOrdering();
             }
         }
         $ordering++;
         $postImg = new \PostImages();
         $postImg->setPath($filePath);
         $postImg->setPostId($postId);
         $postImg->setOrdering($ordering);
         if (!\PostPeer::getPostMainImg($postId)) {
             $postImg->setIsMain(true);
         }
         if ($postImg->save()) {
             $res->type = \AjaxResponse::SUCCESS;
             $t = $postImg->toArray();
             $t['thumb_url'] = $postImg->getThumbs(96, 96);
             $t['url'] = $postImg->getUrl();
             $res->postImage = $t;
             return $this->renderText($res->toString());
         } else {
             foreach ($postImg->getValidationFailures() as $validationFailure) {
                 $error[$validationFailure->getColumn()] = $validationFailure->getMessage();
             }
         }
     } else {
         $error['upload'] = $fileUploader->getError();
     }
     $res->type = \AjaxResponse::ERROR;
     $res->error = $error;
     return $this->renderText($res->toString());
 }
示例#4
0
 public function _save(\Banner $banner, &$error = array())
 {
     $banner->hydrate($this->request()->post('input', 'ARRAY', array()));
     $file_upload = @$_FILES['file_upload']['tmp_name'];
     if ($banner->isNew() && null == $file_upload) {
         @($error['banner.file'] = t('Banner file is required!'));
     }
     if (!$banner->getTermId()) {
         @($error['banner.term_id'] = t('Banner group is required!'));
     }
     if (empty($error)) {
         //upload file
         if ($file_upload) {
             $upload_dir = PUBLIC_DIR . '/media/banner';
             Folder::create($upload_dir, 0777);
             $fileUploader = new Uploader(PUBLIC_DIR . '/media/banner/', 'file_upload');
             $fileUploader->setMaximumFileSize(ini_get('upload_max_filesize'));
             $fileUploader->setFilterType('.jpg, .jpeg, .png, .bmp, .gif');
             $fileUploader->setIsEncryptFileName(true);
             if ($fileUploader->upload()) {
                 $data = $fileUploader->getData();
                 $banner->setFile('/media/banner/' . $data['file_name']);
             } else {
                 $uploadErr = $fileUploader->getError();
                 $error['banner.file'] = implode('.', $uploadErr);
             }
         }
         if (empty($error) && $banner->save()) {
             return true;
         }
         foreach ($banner->getValidationFailures() as $validationFailure) {
             $error[$validationFailure->getColumn()] = $validationFailure->getMessage();
         }
     }
     return false;
 }
示例#5
0
 public function executeUpload()
 {
     $upload_max_filesize = str_replace('M', '', ini_get('upload_max_filesize'));
     $res = new \AjaxResponse();
     $res->type = \AjaxResponse::ERROR;
     $item_id = $this->request()->post('item_id');
     if (!$item_id || !($item = \Items::retrieveById($item_id))) {
         $res->message = t('Items not found');
         return $this->renderText($res->toString());
     }
     $upload_dir = PUBLIC_DIR . '/media/product_img/';
     Folder::create($upload_dir, 0755);
     $error = array();
     $fileUploader = new Uploader($upload_dir, 'file_upload');
     $fileUploader->setMaximumFileSize($upload_max_filesize);
     $fileUploader->setFilterType('.jpg, .jpeg, .png, .bmp, .gif');
     $fileUploader->setIsEncryptFileName(true);
     if ($fileUploader->upload('upload_images')) {
         $data = $fileUploader->getData();
         $file_path = '/media/product_img/' . $data['file_name'];
         $file_url = rtrim($this->document()->getBaseUrl(), '/') . preg_replace('/(\\/+)/', '/', '/../' . $file_path);
         $image = ['file_name' => $data['file_name'], 'thumb_url' => \Items::getImgThumbFromPath($file_path, 52), 'url' => $file_url, 'path' => $file_path, 'ordering' => 0, 'public' => true];
         if (!$item->getMainImgData()) {
             $image['main'] = true;
         }
         $other_img = $item->getImages();
         $last = array_pop($other_img);
         if ($last) {
             $image['ordering'] = (int) @$last['ordering'] + 1;
         }
         $item->addImage($image);
         $item->save(false);
         $res->type = \AjaxResponse::SUCCESS;
         $res->image = $image;
         return $this->renderText($res->toString());
     } else {
         $error['upload'] = $fileUploader->getError();
     }
     $res->error = $error;
     return $this->renderText($res->toString());
 }