Home>Article>Backend Development> How to delete uploaded pictures in php
php method to delete uploaded images: first extract the file domain content name and judge; then obtain the full path and image name of the image; finally delete it through the "unlink("uppic/".$img);" method Just upload the picture.
Recommended: "PHP Video Tutorial"
Simple PHP upload image, delete image implementation code
PHP uploading pictures and deleting pictures implementation code, friends in need can refer to it.
Upload pictures:
The code is as follows:
if (!empty($_FILES["img"]["name"])) { //提取文件域内容名称,并判断 $path=”uppic/”; //上传路径 if(!file_exists($path)) { //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir(“$path”, 0700); }//END IF //允许上传的文件格式 $tp = array(“image/gif”,”image/pjpeg”,”image/jpeg”); //检查上传文件是否在允许上传的类型 if(!in_array($_FILES["img"]["type"],$tp)) { echo “”; exit; }//END IF $filetype = $_FILES['img']['type']; if($filetype == ‘image/jpeg'){ $type = ‘.jpg'; } if ($filetype == ‘image/jpg') { $type = ‘.jpg'; } if ($filetype == ‘image/pjpeg') { $type = ‘.jpg'; } if($filetype == ‘image/gif'){ $type = ‘.gif'; } if($_FILES["img"]["name"]) { $today=date(“YmdHis”); //获取时间并赋值给变量 $file2 = $path.$today.$type; //图片的完整路径 $img = $today.$type; //图片名称 $flag=1; }//END IF if($flag) $result=move_uploaded_file($_FILES["img"]["tmp_name"],$file2); //特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件 }//END IF //这里再将$img的值写入到数据库中对应的字段
Delete pictures:
The code is as follows:
unlink(“uppic/”.$img); //当然,变量的值是从数据库中读取出来的,PHP删除图片比ASP简洁多了
The above is the detailed content of How to delete uploaded pictures in php. For more information, please follow other related articles on the PHP Chinese website!