Home >Backend Development >PHP Tutorial >PHP image upload implementation code with detailed comments_PHP tutorial

PHP image upload implementation code with detailed comments_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:561017browse

Copy code The code is as follows:

//User uploads image processing file
if ((( $_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES[" file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 100000)){ //Control the types of pictures allowed to be uploaded, the last 100000 is Allowed image sizes
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "
"; //Error returns
}else{
/* //This is the information of the uploaded image. You can see the effect by removing the comments before and after.
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $ _FILES["file"]["tmp_name"] . "
"; */
if (file_exists("userupload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],"userupload/ " . $_FILES["file"]["name"]);
}

$date=date('Ymdhis'); //Get the current time, such as; 20070705163148
$fileName =$_FILES['file']['name']; //Get the name of the uploaded file
$name=explode('.',$fileName); //Split the file name with '.' to get the suffix name , get an array
$newPath=$date.'.'.$name[1]; //Get a new file as '20070705163148.jpg', that is, the new path
$oldPath=$_FILES[ 'file']['tmp_name']; //Temporary folder, the previous path
rename("userupload/".$fileName,"userupload/".$newPath);

// You can write your SQL statement here. The address of the image is "userupload/".$newPath

?>

}
}else{
echo "Invalid file"; //The image type is wrong or too large
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321660.htmlTechArticleCopy the code as follows: ?php //User uploads image processing file if ((($_FILES["file" ]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type "...
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