qc.ifish7.com/Application/Admin/Controller/FileController.class.php

161 lines
5.8 KiB
PHP

<?php
namespace Admin\Controller;
use Think\Controller;
class FileController extends AdminController{
public function __construct(){
parent::__construct();
$this->table=D("file");
}
public function index(){
$this->pubid=I("get.pubid");
$this->listtype=I("get.listtype");
$this->file_checkbox=I("get.file_checkbox");//是否多选文件
if(!$this->listtype){
$this->listtype="img";
}
$this->display();
}
//列表模板
public function listtemp(){
$listtype=I("get.listtype");
if($listtype=="file"){
$this->display("file_temp");
}
else if($listtype=="video"){
$this->display("video_temp");
}else{
$this->display("img_temp");
}
}
//信息列表
public function getlist(){
$listtype=I("get.listtype");
$id=I("get.id",0,"int");
$listRows=100;//每页显示多少条
$where=array();
//搜索
$keywords=I("get.keywords");
if($keywords){
$where['name']=array('LIKE','%'.$keywords.'%');
}
//文件类型
$exts=C("upload_".$listtype.'.exts');
//指定pubid
$pubid=I("get.pubid");
if($pubid){
$where['pubid']=$pubid;
}
//id(当文件上传结束后增加一个单位)
if($id){
$where['id']=$id;
}
$exts_arr=explode(",",$exts);
$where['ext']=array('in',$exts_arr);
$page=I("get.page",0,"int")-1;
$page=$page<0?0:$page;
$total=$this->table->where($where)->count();
$start_row=$page*$listRows;
$end_row=($page+1)*$listRows;
$data=$this->table->where($where)->order("name asc")->limit($start_row,$end_row)->select();
$rs=array();
foreach($data as $v){
$v[size]=$this->getsize($v[size]);
$v[create_time]=date("Y/m/d H:i:s",$v[create_time]);
$rs[]=$v;
}
$this->data=$rs;
if($listtype=="file"){
$this->display("file_li");
}
else if($listtype=="video"){
$this->display("video_li");
}else{
$this->display("img_li");
}
}
//用于测试选择文件的
public function demo(){
$this->display();
}
//文件大小
private function getsize($size){
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$units[$i];
}
//文件上传
public function fileupload(){
$listtype=I("get.listtype")?I("get.listtype"):I("post.listtype");//上传类型
//根据不同类型返回上传配置信息
$upload_driver=C("upload_".$listtype.'.upload_driver');
/* 返回标准数据 */
$return = array('status' => 1, 'info' => '上传成功', 'data' => '', 'id' => '');
$setting= array(
'mimes' => C("upload_".$listtype.'.mimes'), //允许上传的文件MiMe类型
'maxSize' => C("upload_".$listtype.'.maxSize')*1024*1024, //上传的文件大小限制 (0-不做限制)
'exts' => C("upload_".$listtype.'.exts'), //允许上传的文件后缀
'autoSub' => true, //自动子目录保存文件
'subName' => array('date', 'Ymd'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
'rootPath' => './d/'.C("upload_".$listtype.'.rootPath').'/', //保存根路径
'savePath' => '', //保存路径
'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
'saveExt' => '', //文件保存后缀,空则使用原后缀
'replace' => false, //存在同名是否覆盖
'hash' => true, //是否生成hash编码
'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
);
/* 调用文件上传组件上传文件 */
$Picture = new \Model\FileModel();
$pic_driver = C('PICTURE_UPLOAD_DRIVER');
if($listtype=="img"){
$thumb=array(200,200);
}else{
$thumb=false;
}
$info = $Picture->upload($_FILES,$setting,$upload_driver,C("UPLOAD_{$upload_driver}_CONFIG"),$thumb); //TODO:上传到远程服务器
/* 记录图片信息 */
if($info){
$return['status'] = 1;
$return['filepath'] = $info['filepath'];
$return['id'] = $info['id'];
} else {
$return['status'] = 0;
$return['info'] = $Picture->getError();
}
/* 返回JSON数据 */
$this->ajaxReturn($return);
}
//删除文件
public function delPicture(){
$ids=I("get.ids");
D("UserLog")->add("delete","db_file",$ids);
$ids_arr=explode(",",$ids);
$file = M("file");
if(count($file)>0){
foreach($ids_arr as $id){
$id=(int)$id;
//根据id查找文件路径
$filers=$file->find($id);
if($filers["filepath"]){
unlink ($_SERVER['DOCUMENT_ROOT'].$filers["filepath"]);//删除文件
}
//缩略图
if($filers["smallpath"]){
unlink ($_SERVER['DOCUMENT_ROOT'].$filers["smallpath"]);//删除文件
}
//
$file->delete($id);
}
}
$state=1;
echo $this->ajaxReturn(array('state'=>$state));
}
}