laravel 上傳視頻 封裝的控制器

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Audio;
use Illuminate\Support\Facades\Input;




class AudioController extends CommonController
{
    //上傳視頻
    public function audio()
    {

        $file = Input::file('file');
        $mimeType = $file->getMimeType(); //文件類型
        $entension = $file -> getClientOriginalExtension(); //上傳文件的後綴.
        $newName = $this->random_file_name().'.'.$entension;
        $path = $file -> move(base_path().'/public/uploads/audio/'.date("Y").'/'.date("m").'/'.date("d"),$newName);//移動文件
        $filepath = 'uploads/audio/'.date("Y").'/'.date("m").'/'.date("d").'/'.$newName;
        $data['title'] = $newName;
        $data['size'] = $this->get_file_size($path);
        $data['type'] = $mimeType;
        $data['url'] = '/'.$filepath;
        $data['delurl'] = 'file='.$newName.'&dt=uploads/audio/'.date("Y").'/'.date("m").'/'.date("d");
        $data['add_time'] = time();

        $uploadData = Audio::create($data);
        if($uploadData){
            $vdata['id'] = $uploadData->id;
            $vdata['delurl'] = $uploadData->delurl;
            $vdata['status'] = 1;
        }else{
            $vdata['status'] = -1;
            $vdata['msg'] = '請求失敗,請重試';
        }

        return response()->json($vdata);
    }

    public function delaudio(){
        if(is_readable(Input::get('dt').'/'.Input::get('file'))){
            if(unlink(Input::get('dt').'/'.Input::get('file'))){
                $re = Audio::where('id',Input::get('delid'))->delete();
                if($re){
                    $vdata['status'] = 1;
                    $vdata['msg'] = '刪除成功';
                }else{
                    $vdata['status'] = -1;
                    $vdata['msg'] = '刪除失敗';
                }
            }else{
                $vdata['status'] = -1;
                $vdata['msg'] = '刪除失敗';
            }
            return response()->json($vdata);
        }
    }



    // ADD by:era 添加隨機命名
    protected function random_file_name() {
        $alphanum = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $len = 5;
        $name= str_replace(".", "", microtime(true));
        for ($i=0; $i<$len; $i++) {
            $name .= $alphanum[rand(0,strlen($alphanum)-1)];
        }
        return strtolower($name);
    }
    //轉換字節
    protected function fix_integer_overflow($size) {
        if ($size < 0) {
            $size += 2.0 * (PHP_INT_MAX + 1);
        }
        return $size;
    }

    protected function get_file_size($file_path, $clear_stat_cache = false) {
        if ($clear_stat_cache) {
            clearstatcache(true, $file_path);
        }
        return $this->fix_integer_overflow(filesize($file_path));
    }



}

 

數據庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章