Home > Article > Backend Development > 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 = 'dirlist.php'; $result = @unlink ($file); if ($result == false) { echo '蚊子赶走了'; } else { echo '无法赶走'; } ?>
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!