首先得注意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>
</td>
</tr>
<tr>
<td height="20" colspan="2" bgcolor="#FFFFFF" class="STYLE6" align="center"><input type="submit" id="mp3_submit" value="确定添加" /></td>
</tr>
</table>
</form>
<!--下面是上传音频的js-->
<script>
//录音上传
$(function () {
$("#test").change(function () {
var objUrl = getObjectURL(this.files[0]);
$("#audio").attr("src", objUrl);
$("#audio")[0].pause();
$("#audio").show();
$("#mp3_submit").show()
getTime();
});
});
<!--获取mp3文件的时间 兼容浏览器-->
function getTime() {
setTimeout(function () {
var duration = $("#audio")[0].duration;
if(isNaN(duration)){
getTime();
}
else{
console.info("该歌曲的总时间为:"+$("#audio")[0].duration+"秒")
}
}, 10);
}
<!--把文件转换成可读URL-->
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
return url;
}
</script>
删
控制器
public function del($id){
$music=D('music');
if($music->delete($id)){
$this->success('音乐删除成功!',U('lst'));
}else{
$this->error('音乐删除失败!');
}
}
视图
<a href="__CONTROLLER__/del/id/{$vo.id}" onclick="return confirm('您确定要删除该音乐吗?');">删除</a>
改
控制器
public function edit(){
$music=D('music');
$id=I('id');
$musics=$music->find($id);
$this->assign('musics',$musics);
//
if(IS_POST){
$data['id']=I('id');
$data['title']=I('title');
$data['author']=I('author');
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->save()){
$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()">
<input type="hidden" name="id" value="{$musics.id}" />
<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" value="{$musics.title}" /></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" value="{$musics.author}" /></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" type="file" />
<if condition="$musics[music] neq ''">
<audio controls src="/{$musics.music}" height="50"></audio>
<else/>
暂无音频
</if>
</td>
</tr>
<tr>
<td height="20" colspan="2" bgcolor="#FFFFFF" class="STYLE6" align="center"><input type="submit" value="确定修改" /></td>
</tr>
</table>
</form>
<script>
//录音上传
$(function () {
$("#test").change(function () {
var objUrl = getObjectURL(this.files[0]);
$("#audio").attr("src", objUrl);
$("#audio")[0].pause();
$("#audio").show();
$("#mp3_submit").show()
getTime();
});
});
<!--获取mp3文件的时间 兼容浏览器-->
function getTime() {
setTimeout(function () {
var duration = $("#audio")[0].duration;
if(isNaN(duration)){
getTime();
}
else{
console.info("该歌曲的总时间为:"+$("#audio")[0].duration+"秒")
}
}, 10);
}
<!--把文件转换成可读URL-->
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
return url;
}
</script>
查
控制器
public function lst(){
$music = M('music'); // 实例化User对象
$count= $music->count();// 查询满足要求的总记录数
$Page= new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$Page->setConfig('prev', '上一页');
$Page->setConfig('next', '下一页');
$show = $Page->show();// 分页显示输出
$list = $music->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display();
}
视图
<form method="post" action="__CONTROLLER__/bdel">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="6%" height="19" valign="bottom"><div align="center"><img src="<?php echo ADMIN_PUC;?>images/tb.gif" width="14" height="14" /></div></td>
<td width="94%" valign="bottom"><span class="STYLE1"> 音乐列表</span></td>
</tr>
</table></td>
<td><div align="right"><span class="STYLE1">
<img src="<?php echo ADMIN_PUC;?>images/add.gif" width="10" height="10" /> <a href="__CONTROLLER__/add"><span>添加</span></a> </span><span class="STYLE1"> </span></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce" onmouseover="changeto()" onmouseout="changeback()">
<tr>
<td width="10%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">id</span></div></td>
<td width="27%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">音乐名</span></div></td>
<td width="10%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">作者</span></div></td>
<td width="14%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">音频</span></div></td>
<td width="14%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">基本操作</span></div></td>
</tr>
<volist name="list" id="vo">
<tr>
<td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19">{$vo.id}</span></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left" style="padding-left:5px;"><?php echo str_repeat('-',$vo[level]*8);?>{$vo.title}</td>
<td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19">{$vo.author}</span></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">
<if condition="$vo[music] neq ''">
<audio controls src="__ROOT__/{$vo.music}" height="50"></audio>
<else />
暂无音频
</if>
</div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center" class="STYLE21"><a href="__CONTROLLER__/edit/id/{$vo.id}">修改</a> | <a href="__CONTROLLER__/del/id/{$vo.id}" onclick="return confirm('您确定要删除该音乐吗?');">删除</a></div></td>
</tr>
</volist>
</table></td>
</tr>
</table>
<div class="pagination">
<ul>
<li>{$page}</li>
</ul>
</div>
</form>