Home > Backend Development > PHP Tutorial > PHP implementation of uploading and deleting images examples

PHP implementation of uploading and deleting images examples

little bottle
Release: 2023-04-06 09:52:02
forward
5502 people have browsed it

Today, the editor will share with you a simple sample code for uploading pictures and deleting pictures in PHP. It is very simple and suitable for reference for beginners. It is also good for studying uploading pictures in PHP. Friends who are interested can learn about it.

1. PHP upload pictures:

<?php
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 “<script>alert(‘格式不对&#39;);history.go(-1);</script>”;
exit;
}//END IF
$filetype = $_FILES[&#39;img&#39;][&#39;type&#39;];
if($filetype == ‘image/jpeg&#39;){
$type = ‘.jpg&#39;;
}
if ($filetype == ‘image/jpg&#39;) {
$type = ‘.jpg&#39;;
}
if ($filetype == ‘image/pjpeg&#39;) {
$type = ‘.jpg&#39;;
}
if($filetype == ‘image/gif&#39;){
$type = ‘.gif&#39;;
}
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的值写入到数据库中对应的字段
Copy after login

2. PHP delete pictures:


##

1 <?php2 
unlink(“uppic/”.$img); //unlink方法删除文件
Copy after login
Have you learned this? Is not it simple!

Related tutorials:

PHP video tutorial

The above is the detailed content of PHP implementation of uploading and deleting images examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template