thinkphp form upload file and save file path to database

不言
Release: 2023-03-25 16:16:01
Original
2656 people have browsed it

This article mainly introduces the relevant information about uploading files through thinkphp forms and saving file paths to the database. Friends in need can refer to

Uploading a single file. This article takes uploading pictures as an example. The effect is as shown in the figure

Create database upload_img to save the upload path

CREATE TABLE `seminar_upload_img` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`img_name` varchar(255) DEFAULT NULL COMMENT '图片名称',
`img_url` varchar(255) DEFAULT NULL COMMENT '图片路径',
`create_time` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
Copy after login

Connect to the database in the public configuration file Common/Conf.php and configure the address

return array(
'DB_TYPE'=>'mysql',
'DB_HOST'=>'127.0.0.1',
'DB_NAME'=>'seminar',
'DB_USER'=>'root',
'DB_PWD'=>'root',
'DB_PORT'=>3306,
'DB_PREFIX'=>'seminar_',
'DB_CHARSET'=>'utf8',
'SHOW_PAGE_TRACE'=>true,
/*地址替换*/
'TMPL_PARSE_STRING'=>array(
'__UPLOAD__'=>__ROOT__.'/Public/Uploads',
),
);
Copy after login

in the view file Upload/index.html

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
#img{height:22px; border:#000 2px solid}
#button{height:30px; width:100px;}
</style>
</head>
<body>
<p><notemply name="data">
<img src="__UPLOAD__/{$data[&#39;img_url&#39;]}" width="200" height="100"/>
</notemply></p>
<p class="result" >上传允许文件类型:&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;图像文件后</p><br>
<form action="{:U(&#39;upload/Upload&#39;)}" method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" value="上传" id="button">
</form>
</body>
</html>
Copy after login

Implementation of uploading files in the controller UploadController.class.php

namespace Home\Controller;
use Think\Controller;
class UploadController extends Controller {
public function index() {
$img=M(&#39;upload_img&#39;);
$sel=$img->order(&#39;create_time desc&#39;)->find();
$this->assign(&#39;data&#39;, $sel);
$this->display();
}
public function upload(){
$upload_img=M(&#39;upload_img&#39;);
if(!empty($_FILES)){
//上传单个图像
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 1*1024*1024 ;// 设置附件上传大小
$upload->exts = array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;);// 设置附件上传类型
$upload->rootPath = &#39;Public/Uploads/&#39;; // 设置附件上传根目录
$upload->savePath = &#39;&#39;; // 设置附件上传(子)目录
$upload->saveName=array(&#39;uniqid&#39;,&#39;&#39;);//上传文件的保存规则
$upload->autoSub = true;//自动使用子目录保存上传文件 
$upload->subName = array(&#39;date&#39;,&#39;Ymd&#39;);
// 上传单个图片
$info = $upload->uploadOne($_FILES[&#39;image&#39;]);
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功 获取上传文件信息
$img_url=$info[&#39;savepath&#39;].$info[&#39;savename&#39;];
$data[&#39;img_url&#39;]=$img_url;
$data[&#39;img_name&#39;]=$info[&#39;savename&#39;];
$data[&#39;create_time&#39;]=NOW_TIME;
$upload_img->create($data);
$result=$upload_img->add();
if(!$result){
$this->error(&#39;上传失败!&#39;);
}else{
$this->success(&#39;上传成功&#39;);
}
}
}
}
}
Copy after login

related Recommended:

ThinkPHP form automatic verification application, thinkphp form verification_PHP tutorial

The above is the detailed content of thinkphp form upload file and save file path to database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template