Home Backend Development PHP Tutorial A complete PHP image upload, thumbnail generation, and database code insertion process_PHP tutorial

A complete PHP image upload, thumbnail generation, and database code insertion process_PHP tutorial

Jul 13, 2016 pm 05:40 PM
php upload under code picture whole insert database yes process generate of write

The following is a complete code written in PHP to upload images while generating thumbnails and inserting them into the database.
<?php
$title = $_POST[title];
$descrīption = $_POST[descrīption];
$albumID = $_POST[albumID];
$iscommend = $_POST[iscommend];
$isvisible = $_POST[isvisible];
$uploadtime = date("Y-m-d H:i:s");
$MAX_FILE_SIZE = floor($ _POST[MAX_FILE_SIZE] / 1024);

$FileExtName = strtolower(pathinfo($_FILES[url][name],PATHINFO_EXTENSION));
if(!(($FileExtName == gif) or ($FileExtName == jpg) or ( $FileExtName == jpeg) or ($FileExtName == png))){
echo "<scrīpt language=javascrīpt>";
echo " alert(The file type you uploaded is wrong, this system only supports PNG , JPG, GIF three formats);";
echo " location=../uploadpicture.php?albumID=$albumID;";
echo "</scrīpt>";
exit;
}
$Filename = date("YmdHis").substr(microtime(),2,5)...$FileExtName;
$PictureDir = substr(pathinfo($_SERVER[scrīpt_FILENAME],PATHINFO_DIRNAME ),0,strlen(pathinfo($_SERVER[scrīpt_FILENAME],PATHINFO_DIRNAME))-8);
$UploadURL = $PictureDir./.$PictureFolder.$Filename;
$ThumbsURL = $PictureDir./.$ ThumbsFolder.$Filename;
if (!move_uploaded_file($_FILES[url][tmp_name], $UploadURL)){
echo "<scrīpt language=javascrīpt>";
echo " alert(Upload failed , the image size cannot exceed $MAX_FILE_SIZE KB. );";
echo " location=../uploadpicture.php?albumID=$albumID;";
echo "</scrīpt>";
exit ;
}
list($RealWidth, $RealHeight) = getimagesize($UploadURL);
if($RealWidth < $MaxThumbWidth){
$percent = 1;
} else {
$percent = $MaxThumbWidth / $RealWidth;
}
$NewWidth = $RealWidth * $percent;
$NewHeight = $RealHeight * $percent;
$thumb = imagecreatetruecolor($NewWidth , $NewHeight);
list($width, $height, $PictureType, $attrib) = getimagesize($UploadURL);
switch($PictureType)
{
case "1": $ source = imagecreatefromgif($UploadURL); break;
case "2": $source = imagecreatefromjpeg($UploadURL); break;
case "3": $source = imagecreatefrompng($UploadURL); break;
}
imagecopyresized($thumb, $source, 0, 0, 0, 0, $NewWidth, $NewHeight, $RealWidth, $RealHeight);
switch($PictureType)
{
case "1": imagegif($thumb, $ThumbsURL); break;
case "2": imagejpeg($thumb, $ThumbsURL); break;
case "3": imagepng($thumb, $ThumbsURL) ; break;
}

$newpicture_SQL = "INSERT INTO pictures (flag,title,url,descrīption,uploadtime,updatetime,isvisible,iscommend) VALUES ($albumID,$title,$Filename,$descrīption,$uploadtime,$uploadtime, $isvisible,$iscommend);";
$setCover = "Update pictures SET url = $Filename WHERE id = $albumID;";
$totalPictures_SQL = "SELECT * FROM pictures WHERE flag = $albumID;";
$totalPictures = mysql_query($totalPictures_SQL, $hesweb) or die(mysql_error());
$totalRows_totalPictures = mysql_num_rows($totalPictures);
$newpicture = mysql_query($newpicture_SQL, $hesweb) or die (mysql_error());
$pictureID = mysql_insert_id($hesweb);
$coverFlag = "UPDATE pictures SET iscover = 1 WHERE id = $pictureID;";
if($totalRows_totalPictures < 1) {
$cover = mysql_query($setCover, $hesweb) or die(mysql_error());
$flag = mysql_query($coverFlag, $hesweb) or die(mysql_error());
}
if($coverFlag){
echo "<scrīpt language=javascrīpt>";
echo " alert(Upload successful.);";
echo " location=../uploadpicture.php? albumID=$albumID;";
echo "</scrīpt>";
}
?>

I hope you can gain something from the above code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486180.htmlTechArticleThe following is a complete code written in PHP to upload images while generating thumbnails and inserting them into the database. ?php $title = $_POST[title]; $descrīption = $_POST[descrīption]; $albumID = $_POST[albu...
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 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 Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

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

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