美文网首页
php -- base64上传文件

php -- base64上传文件

作者: 小哥哥吖Q | 来源:发表于2018-01-19 12:45 被阅读0次

方法可以直接复制用 改变一个目录就可以了

格式

//$base64 = data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgy

  public function base64_image(){

     $base64 =$_POST['img'];  

     $base64_image = str_replace(' ', '+', $base64);//post的数据里面,加号会被替换为空格,需要重新替换回来

     if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result))

        {   

              //匹配成功

              $image_name = uniqid().'.'.$result[2];

            $image_dir="./Public/upload/user_chat/";

            $riqi = date("Ymd");

            $scandir=scandir($image_dir);

            $path=$image_dir.$riqi;

            if(!in_array($riqi,$scandir)){

                mkdir($path,0777,true);

            }

            $image_file=$path.'/'.$image_name;

            //服务器文件存储路径

              if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){

                              return true;

              }else{

                 return false;

              }

          }

 else

  {

           return false; //格式有误

   }

  }

相关文章

网友评论

      本文标题:php -- base64上传文件

      本文链接:https://www.haomeiwen.com/subject/ulyooxtx.html