thinkphp学习笔记(十四)上传文件
需要进行action的设置,包括uploadfile的文件引入
select(); $this->assign('alist',$list); $this->display(); } function upload(){ //经过自定义模型 if (empty($_files)){ $this->error(必须选择上传文件); }else { $msg = $this->up(); if (isset($msg)){ $result=$this->insertdb($msg); if ($result){ $this->success('上传成功'); }else{ $this->error('保存出错了那'); } }else{ $this->error($msg); } } } private function insertdb($data){ $file=m('file'); $result=true; for ($i = 0; $i data($data)->add()){ return false; } } return true; } private function up(){ import('org.net.uploadfile'); $upload=new uploadfile(); $upload->maxsize='1000000';//大小:-1不限制;单位是bytes $upload->savepath='./public/';//上传路径:建议以主路口或者平级目录的子目录来 $upload->saverule='uniqid';//保存规则:默认是uniqid $upload->hashtype='md5';//hash验证方法,默认是md5 $upload->autocheck=false; $upload->uploadreplace=true; $upload->allowexts=array('jpg','png');//允许上传的文件格式 $upload->allowtypes=array('image/png','image/jpg');//文件的mime类型 $upload->thumb=true;//是否开启图片文件缩略图 $upload->thumbmaxwidth='100,500';//可以用,分割,写多个最大宽度 $upload->thumbmaxheight='100,500';//与width一一对应 $upload->thumbprefix='s_,m_';//缩略图文件前缀,与width一一对应// $upload->thumbpath='';//缩略图的保存路径,如果为空直接上传至$upload->savepath// $upload->thumbfile='';//缩略图的文件名,一般不会用,直接用前缀就可以了 $upload->thumbremoveorigin=1;//生成新图后是否删除原图 // $upload->autosub=false;//是否使用子目录进行保存// $upload->subtype='';//子目录创建方式,默认为hash创建,也可以设置为date// $upload->dateformat='';//子目录date方式的指定格式// $upload->hashlevel='';//hash的层级 if ($upload->upload()) { $msg=$upload->getuploadfileinfo(); dump($msg); return $msg; }else { $this->error($upload->geterrormsg()); return $upload->geterrormsg(); } }}?>
html
>>
