php method to delete pictures
Usage scenario: new avatar replaces old one
Steps:
1. Read the URL address of the database avatar,
2. Get the valid fields of the URL address,
3. Set the file path
4. Delete image files
Thinkphp code is as follows:
public function delPic(){ //获取前端传参 用户的uid $uid = I('uid'); if(!$uid) $this->error('uid未获取'); //获取url $url = M('Member')->where(array('uid'=>$uid))->getField('avatar');//$url = 'http://www.test.com/up/avatar/59b25bcfcaac6.jpg' if(!$url) $this->error('数据库获取头像网址失败!'); //获取$url有效字段(去掉网址) $str = substr($url, 20);//$str = 'up/avatar/59b25bcfcaac6.jpg' //file文件路径 $filename = './'.$str; //删除 if(file_exists($filename)){ $info ='原头像删除成功'; unlink($filename); }else{ $info ='原头像没找到:'.$filename; } echo $info; }
Recommended: "PHP Tutorial"
The above is the detailed content of How to delete pictures in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor
