1. 程式人生 > >tp3後臺音訊的增刪改查,tp3上傳音樂

tp3後臺音訊的增刪改查,tp3上傳音樂

首先得注意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> &nbsp;</span><span class="STYLE1"> &nbsp;</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>