zoukankan      html  css  js  c++  java
  • php实现base64图片上传方式实例代码

    <?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 '文件错误';
    }
    

      

  • 相关阅读:
    29
    28
    27
    950. 按递增顺序显示卡牌
    25
    20190624
    409. 最长回文串
    636. 函数的独占时间
    LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
    LeetCode 942. 增减字符串匹配(DI String Match) 49
  • 原文地址:https://www.cnblogs.com/brady-wang/p/8572945.html
Copyright © 2011-2022 走看看