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

How to delete pictures in php?

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

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:

<?php
 
$file = &#39;dirlist.php&#39;;
 
$result = @unlink ($file);
 
if ($result == false) {
 
echo &#39;蚊子赶走了&#39;;
 
} else {
 
echo &#39;无法赶走&#39;; 
} 
?>

Example 2:

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