Home  >  Article  >  php教程  >  通过base64 上传文件

通过base64 上传文件

PHP中文网
PHP中文网Original
2016-05-20 12:58:051503browse

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);
    }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn