uniapp图片上传 - thinkphp6后台接收 - 填坑日记

uniapp图片上传 - thinkphp6后台接收 - 填坑日记,第1张

效果图

多图上传,到自己的腾讯云服务器

主要步骤 组件uni-file-picker,方便获取图片列表的本地路径循环使用uni.uploadFile,逐张上传图片到服务器服务器端tp6使用file('image')和Filesystem::putFile">request()->file('image')和Filesystem::putFile接收图片,返回图片URL

踩过的坑 组件uni-file-picker​默认会自动上传图片到项目绑定的云服务器,不符合需求,需要给组件设置
:auto-upload="false"
来禁止自动上传组件uni-file-picker​绑定变量v-model=“list”一点用都没有,图片列表需要手动通过组件绑定事件
@select="select" @delete=“delete”
来增减算出 真实的图片列表@select="select"事件每次传值,都只是本次多选中的图片,还要手动去合并原有列表手册....坑多uniapp上传接口uni.uploadFile官方手册里,没有写头部信息,导致PHP无法获取
header: {
	'Content-Type': 'multipart/form-data' 
},

thinkphp6文件上传还好

详细步骤

看下面的完整代码吧,心累了

推荐

手机上无广告的百度绿色版首页 baidu.rudon.cn

完整代码

uniapp页面





thinkphp6文件上传接收API


    /**
     * 提交-图片上传
     * 
     */
    public function upload () {
        // 默认返回具体数据
        $res = array();

        // 默认参数
        $fileupload_key = 'file'; // 在uniapp中的 uni.uploadFile({name:'file'}) 设置
        $folder_name_tmp = 'topic'; // 随意、非空,作为接收文件夹名字
        $folder_root = dirname(dirname(dirname(__FILE__))).'/';
        $folder_path_tmp_storage = $folder_root . 'runtime/storage/';
        $folder_public = $folder_root . 'public/';
        $url_root = 'https://xxxxxxxxxxxxxxx/';

        
        // 获取参数
        $targetPath_key = 'path'; // 在uniapp中的 uni.uploadFile({ formData:{path: '/path/to/image/in/server/'} }) 设置
        if (!input('?post.'.$targetPath_key)) {
            return json('缺少参数'.$targetPath_key);
        }
        $subPath = input('post.'.$targetPath_key.'/s');
        $subPath = ltrim($subPath, '/\\');
        
        
        // 获取目标存放路径 - 原始
        // https://www.kancloud.cn/manual/thinkphp6_0/1037639 TP6手册 - 文件上传
        $tmp_file = request()->file( $fileupload_key ); 
        $tmp_savename = Filesystem::putFile( $folder_name_tmp, $tmp_file);

        
        // 获取目标存放路径 - 绝对路径
        $tmp_file_path = realpath($folder_path_tmp_storage . $tmp_savename);
        
        

        // 移动到最终路径
        $target_path = $folder_public . $subPath;
        $target_path_dir = dirname($target_path);
        if (!is_dir( $target_path_dir )) {
            mkdir( $target_path_dir, 0755, true );
        }
        rename($tmp_file_path, $target_path);
        $url = $url_root . $subPath;


        
        // 返回成功信息
        $res['url'] = $url;
        return json($res);
    }

封面

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1296952.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存