PHP头条
热点:

ThinkPHP学习札记(十四)上传文件


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 < count($data); $i++) {
			$data['filename']=$data[$i]['savename'];
			if (!$file->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





<!--{$title}-->



	
	



www.phpzy.comtrue/phprm/15831.htmlTechArticleThinkPHP学习札记(十四)上传文件 ThinkPHP学习笔记(十四)上传文件 需要进行Action的设置,包括UploadFile的文件引入 select();$this->assign('alist',$list);$this->display();}function upload(){//经过自定义...

相关文章

相关频道:

PHP之友评论

今天推荐