Home > Backend Development > PHP Tutorial > How to delete pictures in php?

How to delete pictures in php?

烟雨青岚
Release: 2023-04-08 21:08:01
Original
3685 people have browsed it

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

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

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!

Related labels:
php
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