Home  >  Article  >  Backend Development  >  Simple PHP upload image, delete image implementation code

Simple PHP upload image, delete image implementation code

WBOY
WBOYOriginal
2016-07-29 08:42:441499browse

Upload image:

Copy code The code is as follows:


if (!empty($_FILES["img"]["name"])) { //Extract the file domain content name and determine
$path =”uppic/”; //Upload path
if(!file_exists($path))
{
//Check whether the folder exists, if not, create it and give the highest permissions
mkdir(“$path”, 0700 );
}//END IF
//File formats allowed to be uploaded
$tp = array("image/gif","image/pjpeg","image/jpeg");
//Check whether the uploaded file is allowed Upload type
if(!in_array($_FILES["img"]["type"],$tp))
{
echo “<script>alert('wrong format');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”); //Get the time and assign it to the variable
$file2 = $path.$today.$type; //The full path of the image
$img = $today.$type; //Picture name
$flag=1;
}//END IF
if($flag) $result=move_uploaded_file($_FILES["img"]["tmp_name"],$file2);
//Pay special attention here The first parameter passed to move_uploaded_file is the temporary file uploaded to the server
}//END IF
//Then write the value of $img to the corresponding field in the database


Delete the image:

Copy the code The code is as follows:


unlink(“uppic/”.$img); //Of course, the value of the variable is read from the database. PHP is much simpler to delete pictures than ASP

The above has introduced the simple PHP code to upload pictures and delete pictures, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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