ThinkPHP将上传问件添加到数据库
<?php
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 43145728 ;
$upload->rootPath = './Public/Uploads/';
$info = $upload->upload();
if(!$info) {
$this->error($upload->getError());
}else{
$this->success('上传成功!');
}
$message->time=time();
$message->uid=$_SESSION['id'];
$lastId = $message -> add();
if($lastId){
$this -> success('留言成功');
}else{
$this -> error('留言失败');
}
}
}
?>
namespace Home\Controller; /*****************
use Think\Controller; ****命名空间****/
class MessageController extends Controller {
public function doMess(){$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 43145728 ;
$upload->rootPath = './Public/Uploads/';
$info = $upload->upload();
if(!$info) {
$this->error($upload->getError());
}else{
$this->success('上传成功!');
}
$message = D('Messages');
$message -> create();//必须放在实例化之后
$message->filename = $info['filename']['savename'];//获取上传文件名$message->time=time();
$message->uid=$_SESSION['id'];
$lastId = $message -> add();
if($lastId){
$this -> success('留言成功');
}else{
$this -> error('留言失败');
}
}
}
?>