上傳文件卡住了,記錄一下,本

private $config = [
        'image' => [
            'validate' => [
                'size' => 10*1024*1024,
                'ext'  => 'jpg,png,gif,jpeg',
            ],
            'rootPath'      =>  './Uploads/images/', //保存根路徑
        ],
        'audio' => [
            'validate' => [
                'size' => 100*1024*1024,
                'ext'  => 'mp3,wav,cd,ogg,wma,asf,rm,real,ape,midi',
            ],
            'rootPath'      =>  './Uploads/audios/', //保存根路徑
        ],
        'video' => [
            'validate' => [
                'size' => 100*1024*1024,
                'ext'  => 'mp4,avi,rmvb,rm,mpg,mpeg,wmv,mkv,flv',
            ],
            'rootPath'      =>  './Uploads/videos/', //保存根路徑
        ],
        'file' => [
            'validate' => [
                'size' => 5*1024*1024,
                'ext'  => 'doc,docx,xls,xlsx,pdf,ppt,txt,rar',
            ],
            'rootPath' =>  './Uploads/files/', //保存根路徑
        ],
    ];
    private $domain;
    function __construct()
    {
        //獲取當前域名
        $this->domain = Request::instance()->domain();
    }



    public function upload($fileName){
        if(empty($_FILES) || empty($_FILES[$fileName])){
            return '';
        }
        try{
            $file = request()->file($fileName);
            if (is_array($file)){
                $path = [];
                foreach ($file as $item){
                    $path[] =  $this->save($item);
                }
            } else {
                $path = $this->save($file);
            }
            return $path;
        } catch (\Exception $e){
            $arr = [
                'status' => 0,
                'message' => $e->getMessage(),
            ];
            header('Content-Type: application/json; charset=UTF-8');
            exit(json_encode($arr));
        }
    }


    public function uploadDetail($fileName){
        if(empty($_FILES) || empty($_FILES[$fileName])){
            return [];
        }
        try{
            $file = request()->file($fileName);
            if (is_array($file)){
                $path = [];
                foreach ($file as $item){
                    $detail = $item->getInfo();
                    $returnData['name'] = $detail['name'];
                    $returnData['type'] = $detail['type'];
                    $returnData['size'] = $detail['size'];
                    $returnData['filePath'] = $this->save($item);
                    $returnData['fullPath'] = $this->domain.$returnData['filePath'];
                    $path[] = $returnData;
                }
            } else {
                $detail = $file->getInfo();
                $returnData['name'] = $detail['name'];
                $returnData['type'] = $detail['type'];
                $returnData['size'] = $detail['size'];
                $returnData['filePath'] = $this->save($file);
                $returnData['fullPath'] = $this->domain.$returnData['filePath'];
                $path = $returnData;
            }
            return $path;
        } catch (\Exception $e){
            $arr = [
                'status' => 0,
                'message' => $e->getMessage(),
            ];
            header('Content-Type: application/json; charset=UTF-8');
            exit(json_encode($arr));
        }
    }

    private function getConfig($file){
        $name = pathinfo($file['name']);
        $end = $name['extension'];
        foreach ($this->config as $key=>$item){
            if ($item['validate']['ext'] && strpos($item['validate']['ext'], $end) !== false){
                return $this->config[$key];
            }
        }
        return null;
    }
    private function save(&$file){
        $config = $this->getConfig($file->getInfo());
        if (empty($config)){
            throw new Exception('上傳文件類型不被允許!');
        }
        // 移動到框架應用根目錄/uploads/ 目錄下
        if ($config['validate']) {
            $file->validate($config['validate']);
            $result = $file->move($config['rootPath']);
        } else {
            $result = $file->move($config['rootPath']);
        }
        if($result){
            $path = $config['rootPath'];
            if (strstr($path,'.') !== false){
                $path = str_replace('.', '', $path);
            }
            return $path.$result->getSaveName();
        }else{
            // 上傳失敗獲取錯誤信息
            throw new Exception($file->getError());
        }
    }

 

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