php base64转图片

源代码
public function upload2(){
        $pid = $_SESSION['cmpid'];
        $model='communication';
        $categpry='img';
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $this->post->file, $result)) {


            $ramd = $this->loadModel('businesscard')->random(6, 0);
            $type = $result[2];

            $new_file ='slidimg/'.time() . $ramd . '.' . $type;

//            $new_file = "./test.{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $this->post->file)))) {
//                $ym = date('Ym', time());
//                $upload_path = $this->app->getAppRoot() . "www/data/upload/" . $model . '/' . $categpry . '/' . $ym . "/"; //上传文件的存放路径
//                //开始移动文件到相应的文件夹
//                if (!file_exists($upload_path)) {
//                    mkdir($upload_path, 0777, true);//创建目录
//                }
//                if (copy('/module/communication/1512122578eT7Vin.jpeg','/www/data/upload/communication/img/201712/s.jpeg')) {
//                    echo  "ok";
//                } else {
//                    echo 'false';
//                }

                $this->dao->insert(TABLE_CSLIDERIMG)
                    ->set('url')->eq('module/communication/'.$new_file)
                    ->set('create_time')->eq(helper::now(time()))
                    ->set('cpid')->eq($pid)
                    ->exec();

                echo '新文件保存成功:', $new_file;
            }
        }
//            echo 'xxx----' . $a;
//        $pid = ‘123124afeewrt234’;
//        $upload=$this->communication->uploadimg($pid);
//        echo $upload;
    }

posted @ 2020-07-18 16:57  办公魔盒  阅读(567)  评论(0)    收藏  举报