Home > Backend Development > PHP Problem > How to delete pictures from server in php

How to delete pictures from server in php

藏色散人
Release: 2023-03-04 08:54:02
Original
2951 people have browsed it

The implementation method of php deleting images from the server: first read the URL address of the database image; then obtain the valid fields of the URL address; then set the file path; finally delete the image file directly.

How to delete pictures from server in php

Recommendation: "PHP Video Tutorial"

php Delete the server specified directory images

Usage scenario: new avatar replaces old one

Steps:

1. Read the URL address of the database avatar,

2. Get the URL address Valid fields,

3. File file path setting

4. Delete image file

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;
}
Copy after login

The above is the detailed content of How to delete pictures from server in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template