首页 > Web开发 > 详细

海豚框架之单文件上传

时间:2019-11-28 16:42:56      阅读:101      评论:0      收藏:0      [点我收藏+]

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

写一个公共函数用于上传文件,例如写在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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!