Home  >  Article  >  Backend Development  >  How to delete pictures in php?

How to delete pictures in php?

烟雨青岚
烟雨青岚Original
2020-06-13 10:01:083453browse

How to delete pictures in php?

How to delete pictures in php: Deleting pictures in php is very simple, use unlink functionSimple operation

Example 1:

Example 2:

/**
* 删除图片或文件
* @author Zhenwei Zhang <772979140@qq.com>
* @param string $pic 图片或文件地址
*/
function delpic($pic){
  if($pic){
    $url = strlen('http://'.$_SERVER['HTTP_HOST'].'/');
    $ifurl = strpos($pic,'uploads');
    if($ifurl>10){
      if(file_exists(substr($pic, $url))){
        $res = unlink(substr($pic, $url));
        if($res){
          $return = array('success'=>1,'errors'=>'删除图片或文件成功');
        }else{
          $return = array('success'=>0,'errors'=>'操作失误导致图片或文件无法删除');
        }
      }else{
        $return = array('success'=>404,'errors'=>'无法找到文件或者已经删除');
    }
  }else{
    if(file_exists($pic)){
      $res = unlink($pic);
        if($res){
          $return = array('success'=>1,'errors'=>'删除图片成功');
        }else{
          $return = array('success'=>0,'errors'=>'操作失误导致图片或文件无法删除');
        }
      }else{
        $return = array('success'=>404,'errors'=>'无法找到文件或者已经删除');
      }
    }
  }else{
    $return = array('success'=>404,'errors'=>'请传送正确图片或文件地址');
  }
  return $return;
}

Recommended tutorial: "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!

Statement:
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