How to delete server pictures in php

php delete server specified directory picture

Use scenario: replace the old avatar with the new avatar

Steps:

1. Read the URL address of the database avatar,

2. Get the valid field of the URL address,

3. File file path setting

4. Delete the image file

Thinkphp The code is as follows :

Public function delPic(){     //Get the uid of the user who passed the front-end parameter     $uid = I('uid');     if(!$uid) $this->error('uid not obtained');     //Get url     $ url = M('Member')->where(array('uid'=>$uid))->getField('avatar');//$url ='http://www.zjkweiqi.cn/ up/ avatar/59b25bcfcaac6.jpg' if(!$url) $this->error('Database failed to obtain avatar URL!');     //Get valid fields of $url (remove URL)     $str = substr($url, 20); //$str ='up/avatar/59b25bcfcaac6.jpg'     //file file path     $filename ='. /'.$str ;     //删除     if(file_exists($filename)){

























        $info ='Original profile picture was deleted successfully';

        unlink($filename);

    }else{         $info ='Original profile picture not found:'.$filename;     }     echo $info; }








 

Guess you like

Origin blog.csdn.net/qq_41608099/article/details/107929590