欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

通过base64 上传文件

程序员文章站 2022-04-12 22:21:15
...
1.代码
$url = "http://127.0.0.1/user/update_user_profile.php";
    $post_data = [
        'user_id'=>'100010',
        'sign'=>'1d4d8920ce87b5ef44a67870556dd35a',
        'category_type'=>'1',
        'title'=>'title1',
        'source'=>'source111',
        'content'=>'content1111',
        'image_list'=>'data:image/png;base64,iVBOElFTkSuQmCC|w*h',
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    curl_close($ch);
    //打印获得的数据
    print_r($output);

2. 处理客户端提交的post数据

foreach($param_image_list as $param_image_item) {
        $param_image_data = $param_image_item['image_data'];
        $param_image_width = $param_image_item['w'];
        $param_image_height = $param_image_item['h'];
        $s = base64_decode(str_replace('data:image/png;base64,', '', $param_image_data));
        $image_path = '../uploadimg/'.md5($param_user_id).'_'.time().'_'.$index.'.png';
        file_put_contents($image_path, $s);
        $index ++;
        array_push($image_info_list,$image_path.'|'.$param_image_width.'|'.$param_image_height);
    }