php implement changes to the pictures uploaded

In my code to test session, I ran into a bug, when I modify operations on the data, data submitted directly when I, my image upload field directly into a null value of
before modifying operations information database fields:
Here Insert Picture Description
when I Android operating data submitted directly modify other fields did not change, but my img data fields are as follows:
Here Insert Picture Description
in the case you can see, I am making any unmodified data modification operations, my img field was changed to a null value
Here Insert Picture Description
there is this operation? ? ? ?

Solution:
First look at my previous modification operations (here only shows the code I uploaded the pictures, others omitted):

  public function edit(){
        $data['img'] = I('post.img');
        $name=time();
       if(!empty($data['img'])){
           //目录
           $rootPath = './Public/data/hr/xuelizhengshu/';
           $path = 'data/hr/xuelizhengshu/';
           $data['img']=base64_decode($data['img']);
           file_put_contents($rootPath.$name.'.jpg',$data['img']);
           $data['img']=$path.$name.'.jpg';
         

       }
}

As can be seen from the above, when I modify the operation does not modify the data, my img field is empty, he will not enter the function body, but if the value of the database when I gave my first assignment img field, then ( I finished this operation in the Android studio in), he will enter the human body function, but he would img modify our previous data to the current format of the time stamp, it causes the picture still does not show up,
then I think the best of both worlds method (here may I say a bit chaotic, readers can directly look at the code, the code I will add detailed comments)

  public function edit(){
        $data['img'] = I('post.img');
        $name=time();
       if(!empty($data['img'])){
        if(strpos($data['img'],'.jpg')!==false){//strpos()判断字符串是否包含某个字符串
            //这里的意思就是当不进行修改操作的话,直接将数据库中的img字段重新写入img字段
           $data['img'] = $data['img'];
         }else {
           //目录
           $rootPath = './Public/data/hr/xuelizhengshu/';
           $path = 'data/hr/xuelizhengshu/';
           $data['img']=base64_decode($data['img']);
           file_put_contents($rootPath.$name.'.jpg',$data['img']);
           $data['img']=$path.$name.'.jpg';
         
         }
       }
}

Eau, which is to achieve a modification of the operation of the image upload
Here Insert Picture Description

Published 43 original articles · won praise 60 · views 6737

Guess you like

Origin blog.csdn.net/yuhang01/article/details/103764072