laravel 上传视频 封装的控制器

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 11:13   15   0
<?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));
    }



}

数据库

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP