海豚框架之单文件上传

我在网上找到的关于文件上传部分,然后记录下来,方便下次使用。

写一个公共函数用于上传文件,例如写在function.php文件中,而不是直接修改common.php文件

直接上代码,首先是函数部分:

参数部分:用户id,文件,你也可以设置文件大小和上传路径

function post_upload_one($uid,$file'){

    $file = request()->file($file);
    if (empty($file)) {
        $data['code'] = '2';
        $data['msg'] = '未选择图片';
        return $data;
    }
    $size=11548484$path='/uploads/apifile';

    $info = $file->move(Env::get('root_path') .'/public'.$path.DIRECTORY_SEPARATOR);
    if($info) {
        // 获取附件信息
        $file_info = [
            'uid' => $uid,
            'name' => $file->getInfo('name'),
            'mime' => $file->getInfo('type'),
            'path' => str_replace('\', '/','uploads/apifile' . DIRECTORY_SEPARATOR . str_replace('\', '/', $info->getSaveName()) ),
            'ext' => $info->getExtension(),
            'size' => $info->getSize(),
            'md5' => $info->hash('md5'),
            'sha1' => $info->hash('sha1'),
            'module' => 'user',

        ];
        if ($file_add = AttachmentModel::create($file_info)) {
            $data['code'] = '0';
            $data['msg'] = $file_add->id;
            return $data;
        }
    }
    $data['code'] = '1';
    $data['msg'] = $file->getError();
    return $data;
}

然后是调用我们写好的公共函数

 $data['image']=$_FILES;
 $cc = post_upload_one($uid,'image');
原文地址:https://www.cnblogs.com/cyk2/p/11950810.html