首先得注意php.ini里的 post_max_size 和 upload_max_filesize 值得设置
增
模型
<?php
namespace Admin\Model;
use Think\Model;
class MusicModel extends Model {
}
控制器
public function add(){
$music=D('music');
//添加操作
if(IS_POST){
$data['title']=I('title');
$data['author']=I('author');
$data['time']=time();
if($_FILES['music']['tmp_name']!=''){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 31457280 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg','mp3');// 设置附件上传类型
$upload->rootPath='./';
$upload->savePath = './Public/Uploads/'; // 设置附件上传目录
$info=$upload->uploadOne($_FILES['music']);
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
$data['music']=$info['savepath'].$info['savename'];
}
}
if($music->create($data)){
if($music->add()){
$this->success('音乐新增成功',U('lst'));
}else{
$this->error('音乐新增失败!');
}
}else{
$this->error($music->getError());
}
return;
}
$this->display();
}
视图
<form method="post" action="" enctype="multipart/form-data">
<table class="tbl" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce" onmouseover="changeto()" onmouseout="changeback()">
<tr>
<td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">音乐名</td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="title" type="text" /></td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">作者</td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="author" type="text" /></td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">音频</td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left">
<input name="music" id="test" type="file" />
<audio id="audio" controls autoplay="" style="display: none; "></audio>