Пример #1
0
 /**
  * 保存前要处理的操作
  *
  */
 protected function _before_save()
 {
     //是否不需要上传图片
     if (is_null($this->ico)) {
         return;
     }
     // 是否通过验证
     if (!$this->ico->isValid()) {
         throw new QDB_ActiveRecord_ValidateFailedException(array('ico' => '图标未通过验证'), $this);
     }
     // 添加图片 创建目录
     $dir = rtrim(Q::ini('appini/teapics/upload_dir'), '/\\') . DS;
     $dest_dir = $dir . 'links';
     Helper_Filesys::mkdirs($dest_dir);
     $md5 = md5(rand() . '-' . microtime(true));
     //上传图片
     $img_url = $md5 . '.' . $this->ico->extname();
     $this->ico->move($dest_dir . DS . $img_url);
     $this->ico = 'links' . DS . $img_url;
 }
Пример #2
0
 /**
  * 保存记录前要处理的操作
  *
  */
 protected function _before_save()
 {
     //属性没有值返回
     if (is_null($this->_postfile)) {
         return;
     }
     if (!$this->_postfile->isValid()) {
         throw new QDB_ActiveRecord_ValidateFailedException(array('postfile' => 'postfile is invalid.'), $this);
     }
     // 添加图片
     $dir = rtrim(Q::ini('appini/teapics/upload_dir'), '/\\') . DS;
     $date = date('Y-m');
     $dest_dir = $dir . $date;
     Helper_Filesys::mkdirs($dest_dir);
     $md5 = md5(rand() . '-' . microtime(true));
     //上传图片
     $img_url = $md5 . '.' . $this->_postfile->extname();
     $this->_postfile->move($dest_dir . DS . $img_url);
     $img_url = "{$date}/{$img_url}";
     //生成缩略图
     $thumb_name = $md5 . '-thumb.' . '.jpg';
     $img = Helper_Image::createFromFile($this->_postfile->filePath(), $this->_postfile->extname());
     $width = intval(Q::ini('appini/teapics/thumb_pic_width'));
     $height = intval(Q::ini('appini/teapics/thumb_pic_width'));
     $img->crop($width, $height);
     $fileName = $dest_dir . DS . $thumb_name;
     $img->saveAsJpeg($fileName);
     $thumb_name = "{$date}/{$thumb_name}";
     //是否数据库有图片
     if ($this->thumb_filename && $this->thumb_filename != $thumb_name) {
         //待删除的图片为数据库的图片
         $this->_pending_delete_files['thumb'][] = $this->thumb_filename;
     }
     $this->thumb_filename = $thumb_name;
     if ($this->img_url && $this->img_url != $img_url) {
         $this->_pending_delete_files['img'][] = $this->img_url;
     }
     //赋值到img_url
     $this->img_url = $img_url;
 }
Пример #3
0
 function actionIndex()
 {
     $this->_pathway->addStep('素材上载');
     if ($this->_context->isPOST()) {
         if (isset($_POST["PHPSESSID"])) {
             session_id($_POST["PHPSESSID"]);
         }
         if (!isset($_FILES["filedata"]) || !is_uploaded_file($_FILES["filedata"]["tmp_name"]) || $_FILES["filedata"]["error"] != 0) {
             return '上传失败!';
         }
         $filePath = rtrim(Q::ini('appini/upload/filePath'), '/\\') . DS;
         Helper_Filesys::mkdirs($filePath);
         //获得上传文件夹
         $dir = 'data1';
         $i = 0;
         $handle = opendir($filePath);
         while ($name = readdir($handle)) {
             if ($name != "." && $name != "..") {
                 if (is_dir($filePath . $name) && substr($name, 0, 4) == 'data') {
                     $i++;
                     $dir = $name;
                 }
             }
         }
         closedir($handle);
         if ($i == 0) {
             Helper_Filesys::mkdirs($filePath . $dir);
         }
         //判断文件中的文件是否超出限制
         $j = 0;
         $handle = opendir($filePath . $dir);
         while ($name = readdir($handle)) {
             if ($name != "." && $name != "..") {
                 $j++;
             }
         }
         closedir($handle);
         if ($j > 65535) {
             $dir = 'data' . ($i + 1);
         }
         //得到编码后的文件夹及文件名
         $fileNameMd5 = md5($_FILES["filedata"]["name"] . '-' . microtime(true));
         $filePath .= $dir . DS . $fileNameMd5 . DS;
         //保存路径名
         $fileName = md5_file($_FILES["filedata"]["tmp_name"]);
         //文件名
         $fileExt = pathinfo($_FILES["filedata"]["name"], PATHINFO_EXTENSION);
         //扩展名
         //保存到数据库
         $file = new Files();
         $file->category_id = $this->_context->category_id;
         $file->category_name = $this->_context->category_name;
         $file->title = substr($_FILES["filedata"]["name"], 0, strrpos($_FILES["filedata"]["name"], '.'));
         $file->name = $fileName;
         $file->ext = $fileExt;
         $file->size = $_FILES["filedata"]["size"];
         $file->path = $filePath;
         $file->status = 0;
         $file->catalog_info = '';
         $file->upload_username = $this->_view['currentUser']['username'];
         $file->upload_at = time();
         try {
             $file->save();
         } catch (QDB_ActiveRecord_ValidateFailedException $ex) {
             if (isset($ex->validate_errors['name'])) {
                 return $ex->validate_errors['name'];
             } else {
                 if (isset($ex->validate_errors['type'])) {
                     return $ex->validate_errors['type'];
                 } else {
                     return '上传失败!' . $ex;
                 }
             }
         }
         //保存上传文件
         Helper_Filesys::mkdirs($filePath);
         if (!move_uploaded_file($_FILES["filedata"]["tmp_name"], $filePath . $fileName . '.' . $fileExt)) {
             $file->destroy();
             //保存文件失败回滚数据
             return '上传失败!';
         }
         //返回成功结果
         return 'true_' . url('admin::filecatalog/preview', array('id' => $file->id()));
     } else {
         $categoryId = $this->_context->category_id;
         $categoryId = isset($categoryId) ? $categoryId : 1;
         $category = Category::find()->getById($categoryId);
         $this->_view['category'] = $category;
         $categoryIds = Category::getChildrenIds($categoryId);
         if (count($categoryIds)) {
             //获得历史上传
             $files = Files::find('category_id in (?) and upload_username=?', $categoryIds, $this->_view['currentUser']['username'])->order('upload_at desc')->top(13)->getAll();
             $this->_view['files'] = $files;
         }
     }
 }
Пример #4
0
 /**
  * 执行上传文件
  *
  * @param QDB_ActiveRecord_Abstract $obj
  */
 function _exec_upload(QDB_ActiveRecord_Abstract $obj)
 {
     if (empty($this->_settings['upload_config'])) {
         return;
     }
     $uploader = new Helper_Uploader();
     $error = array();
     foreach ($this->_settings['upload_config'] as $file_id => $config) {
         //必需
         $post_key = isset($config['post_key']) ? $config['post_key'] : $file_id;
         if (!$uploader->existsFile($post_key)) {
             if (isset($config['required']) && $config['required']) {
                 if ($obj->id() && !empty($obj->{$file_id})) {
                     $obj->willChanged($file_id);
                 } else {
                     $error[$post_key]['required'] = $config['errmsgs']['required'];
                 }
             }
             continue;
         }
         $file = $uploader->file($post_key);
         $filename = $file->filename();
         $extname = $file->extname();
         // 验证文件类型
         if (isset($config['allowed_filetypes']) && $config['allowed_filetypes']) {
             if (!$file->isValid($config['allowed_filetypes'])) {
                 $error[$post_key]['allowed_filetypes'] = $config['errmsgs']['allowed_filetypes'];
                 continue;
             }
         } else {
             if ($file->isValid('.php')) {
                 $error[$post_key]['allowed_filetypes'] = "PHP 文件是不允许上传的.";
                 continue;
             }
         }
         //验证文件大小
         if (isset($config['max_size']) && $config['max_size'] > 0) {
             if ($file->filesize() > $config['max_size'] * 1024) {
                 $error[$post_key]['max_size'] = $config['errmsgs']['max_size'];
                 continue;
             }
         }
         // 验证图片尺寸
         if (isset($config['image_dimension']) && $config['image_dimension']) {
             list($width, $height, $type, $attr) = getimagesize($file->filepath());
             if (!$width || !$height) {
                 continue;
             }
             list($dim_width, $dim_height) = explode('*', $config['image_dimension']);
             if (isset($dim_width) && $dim_width > 0 && $dim_width != $width || isset($dim_height) && $dim_height > 0 && $dim_height != $height) {
                 $error[$post_key]['image_dimension'] = $config['errmsgs']['image_dimension'];
                 continue;
             }
         }
         $dir = rtrim($config['upload_dir'], '/\\') . DS;
         $date = date('Y-m');
         $dest_dir = $dir . $date;
         Helper_Filesys::mkdirs($dest_dir);
         $md5 = md5($filename . '-' . microtime(true));
         $_prop_filename = '';
         //如果上图片
         if (isset($config['is_image']) && $config['is_image'] || isset($config['large']) || isset($config['thumb'])) {
             $pic_filename = $md5 . '.jpg';
             $image = Helper_Image::createFromFile($file->filepath(), $file->extname());
             // 生成大图
             if (isset($config['large'])) {
                 list($w, $h) = explode('*', $config['large']);
                 $image->crop($w, $h);
             }
             $image->saveAsJpeg($dest_dir . DS . $pic_filename);
             $_prop_filename = "{$date}/{$pic_filename}";
             // 生成缩略图
             if (isset($config['thumb'])) {
                 $thumb_filename = $md5 . '-thumb.jpg';
                 list($w, $h) = explode('*', $config['thumb']);
                 $image->crop($w, $h);
                 $image->saveAsJpeg($dest_dir . DS . $thumb_filename);
             }
             // 销毁图片资源并删除上传文件
             $image->destroy();
             $file->unlink();
         } else {
             $file_name = $md5 . '.' . $extname;
             $file->move($dest_dir . DS . $file_name);
             $_prop_filename = "{$date}/{$file_name}";
         }
         //如果是更新,删除原有的
         if ($obj->id() && !empty($obj->{$file_id})) {
             $this->_delete_upload_file($obj, $file_id);
         }
         //向属性负值
         $obj->{$file_id} = $_prop_filename;
     }
     if (!empty($error)) {
         throw new QDB_ActiveRecord_ValidateFailedException($error, $obj);
     }
 }