thinkphp表单上传文件并将文件路径保存到数据库中?

thinkphp表单上传文件并将文件路径保存到数据库中


函数式编程
浏览 1256回答 1
1回答

青春有我

上传单个文件,此文以上传图片为例,上传效果如图所示 &nbsp;&nbsp;&nbsp;创建数据库upload_img,用于保存上传路径 &nbsp;&nbsp; &nbsp; &nbsp;CREATE TABLE `seminar_upload_img` ( &nbsp;`id` int(11) NOT NULL AUTO_INCREMENT, &nbsp;`img_name` varchar(255) DEFAULT NULL COMMENT '图片名称', &nbsp;`img_url` varchar(255) DEFAULT NULL COMMENT '图片路径', &nbsp;`create_time` text, &nbsp;PRIMARY KEY (`id`) &nbsp;) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8; &nbsp; &nbsp;在公共配置文件Common/Conf.php中连接数据库,并配置地址 &nbsp;&nbsp; &nbsp; &nbsp;return array( &nbsp;'DB_TYPE'=>'mysql', &nbsp;'DB_HOST'=>'127.0.0.1', &nbsp;'DB_NAME'=>'seminar', &nbsp;'DB_USER'=>'root', &nbsp;'DB_PWD'=>'root', &nbsp;'DB_PORT'=>3306, &nbsp;'DB_PREFIX'=>'seminar_', &nbsp;'DB_CHARSET'=>'utf8', &nbsp;'SHOW_PAGE_TRACE'=>true, &nbsp;/*地址替换*/ &nbsp;'TMPL_PARSE_STRING'=>array( &nbsp;'__UPLOAD__'=>__ROOT__.'/Public/Uploads', &nbsp;), &nbsp;); &nbsp; &nbsp;视图文件Upload/index.html中 &nbsp;&nbsp; &nbsp; &nbsp;<!DOCTYPE html> &nbsp;<html> &nbsp;<head> &nbsp;<title></title> &nbsp;<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> &nbsp;<style type="text/css"> &nbsp;#img{height:22px; border:#000 2px solid} &nbsp;#button{height:30px; width:100px;} &nbsp;</style> &nbsp;</head> &nbsp;<body> &nbsp;<div><notemply name="data"> &nbsp;<img src="__UPLOAD__/{$data['img_url']}" width="200" height="100"/> &nbsp;</notemply></div> &nbsp;<div class="result" >上传允许文件类型:'jpg', 'gif', 'png', 'jpeg'图像文件后</div><br> &nbsp;<form action="{:U('upload/Upload')}" method="post" enctype="multipart/form-data"> &nbsp;<input type="file" name="image"/> &nbsp;<input type="submit" value="上传" id="button"> &nbsp;</form> &nbsp;</body> &nbsp;</html> &nbsp; &nbsp;控制器UploadController.class.php中实现上传文件 &nbsp;&nbsp; &nbsp; &nbsp;namespace Home\Controller; &nbsp;use Think\Controller; &nbsp;class UploadController extends Controller { &nbsp;public function index() { &nbsp;$img=M('upload_img'); &nbsp;$sel=$img->order('create_time desc')->find(); &nbsp;$this->assign('data', $sel); &nbsp;$this->display(); &nbsp;} &nbsp;public function upload(){ &nbsp;$upload_img=M('upload_img'); &nbsp;if(!empty($_FILES)){ &nbsp;//上传单个图像 &nbsp;$upload = new \Think\Upload();// 实例化上传类 &nbsp;$upload->maxSize = 1*1024*1024 ;// 设置附件上传大小 &nbsp;$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 &nbsp;$upload->rootPath = 'Public/Uploads/'; // 设置附件上传根目录 &nbsp;$upload->savePath = ''; // 设置附件上传(子)目录 &nbsp;$upload->saveName=array('uniqid','');//上传文件的保存规则 &nbsp;$upload->autoSub = true;//自动使用子目录保存上传文件 &nbsp; $upload->subName = array('date','Ymd'); &nbsp;// 上传单个图片 &nbsp;$info = $upload->uploadOne($_FILES['image']); &nbsp;if(!$info) {// 上传错误提示错误信息 &nbsp;$this->error($upload->getError()); &nbsp;}else{// 上传成功 获取上传文件信息 &nbsp;$img_url=$info['savepath'].$info['savename']; &nbsp;$data['img_url']=$img_url; &nbsp;$data['img_name']=$info['savename']; &nbsp;$data['create_time']=NOW_TIME; &nbsp;$upload_img->create($data); &nbsp;$result=$upload_img->add(); &nbsp;if(!$result){ &nbsp;$this->error('上传失败!'); &nbsp;}else{ &nbsp;$this->success('上传成功'); &nbsp;} &nbsp;} &nbsp;} &nbsp;} &nbsp;} &nbsp; &nbsp;以上所述是小编给大家介绍的thinkphp表单上传文件并将文件路径保存到数据库中,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
打开App,查看更多内容
随时随地看视频慕课网APP