Home Backend Development PHP Tutorial Simple example of uploading and deleting images using PHP

Simple example of uploading and deleting images using PHP

May 18, 2018 pm 03:45 PM

Simple PHP code to upload pictures and delete pictures

Upload pictures:

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(‘格式不对');history.go(-1);</script>”; 
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的值写入到数据库中对应的字段
Copy after login

Delete pictures:
unlink("uppic/". $img); //Of course, the value of the variable is read from the database. Deleting pictures in PHP is much simpler than ASP

The above is the content of a simple example of uploading pictures and deleting pictures in PHP. For more related content, please pay attention to the PHP Chinese website (www.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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles