todo-php-file-upload

版权声明:如果转载请注明出处,不喜勿喷,谢谢 https://blog.csdn.net/qq_38358436/article/details/89410438
 /**
     * 上传文件(不能使用ThinkPHP提供的文件上传类,过滤了通过formdata上传的文件)
     *
     * @param $file
     */
    public function uploadFile($fileinfo)
    {
        // 获取文件上传配置
        $cfg = config('upload_options');

        $fileName = $fileinfo['name'];

        // 判断文件后缀名
        $fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
        if (!in_array($fileExt, $cfg['allow_type'])) {
            return ['status' => false, 'message' => '不允许该类型的文件'];
        }

        // 判断文件大小
        if ($fileinfo['size'] > $cfg['max_size']) {
            return ['status' => false, 'message' => '文件过大'];
        }

        // 判断是否是通过http请求上传的文件
        if (is_uploaded_file($fileName)) {
            return ['status' => false, 'message' => '非法上传'];
        }

        // 按天生成文件夹, 生成随机文件名
        $cfgPath = trim($cfg['save_path'], '/').'/'.date('Ymd');
        $savePath = rtrim(str_replace('\\', '/', ROOT_PATH), '/').'/public/'.$cfgPath;
        if (!file_exists($savePath)) {
            mkdir($savePath, 0777);
        }

        // 上传文件
        $fileName = md5(time().mt_rand(0, 9999)).'.'.$fileExt;
        if (move_uploaded_file($fileinfo['tmp_name'], $savePath.'/'.$fileName)) {
            return ['status' => true, 'message' => '上传成功', 'path' => $cfgPath.'/'.$fileName];
        } else {
            return ['status' => false, 'message' => '上传失败'];
        }

    }

猜你喜欢

转载自blog.csdn.net/qq_38358436/article/details/89410438