以0字节上传的base64图像,在php

时间:2019-03-06 15:30:50

标签: php base64 image-uploading file-put-contents

我正在尝试使用file_put_contents()函数在for循环中上传多个base64图像,但是 有时 文件正在以 0个字节的形式上传 图片和file_put_contents()函数返回false。如何解决呢?感谢帮助。

$filename = 'appimages/'.date('Y-m-d').'/'.$user_id.'/'.$clientCode;
                if (!file_exists($filename)) {
                    mkdir($filename, 0777, true);
                }

$id = $db->lastInsertId();
                $error = 0;
                $ef = 0;
                foreach ($images as $img) {
                    if(!empty($img)){
                        $image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $img->imgs));
                        // $image = base64_decode($img->imgs);
                        $image_path = $filename.'/'.mt_rand( 100000 , 999999 ).'.png';
                        if(file_put_contents($image_path, $image)){
                            $main_image_path = 'https://consult-trico.com/bank/api/'.$image_path; 

                            $stmt11 = $db->prepare("INSERT INTO daily_transaction_images(dt_ID, deposit_slip_no, image, created_at) VALUES(:dt_ID,:deposit_slip_no,:image,:created_at)");

                            $stmt11->execute(['dt_ID' => $id,'deposit_slip_no' => $img->name, 'image' => $main_image_path, 'created_at' => date('Y-m-d H:i:s')]);
                        }else{
                           $error++; 
                       }
                   }
                   ++$ef;
               }

0 个答案:

没有答案