zl程序教程

您现在的位置是:首页 >  后端

当前栏目

php实现base64图片上传方式实例代码

实例PHP上传代码 实现 方式 图片 base64
2023-09-14 09:12:15 时间
<?php
/**
 * base64图片上传
 * @param $base64_img
 * @return array
 */
header("content-type:text/html;charset=utf-8");
$base64_img = trim($_POST['img']);
$up_dir = './upload/';//存放在当前目录的upload文件夹下

if(!file_exists($up_dir)){
    mkdir($up_dir,0777);
}

if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
    $type = $result[2];
    if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
        $new_file = $up_dir.date('YmdHis_').'.'.$type;
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
            $img_path = str_replace('../../..', '', $new_file);
            echo '图片上传成功</br>![](' .$img_path. ')';
        }else{
            echo '图片上传失败</br>';

        }
    }else{
        //文件类型错误
        echo '图片上传类型错误';
    }

}else{
    //文件错误
    echo '文件错误';
}