A complete PHP image upload, generating thumbnails, and inserting database code flow _ PHP Tutorial

Source: Internet
Author: User
Upload a complete PHP image, generate a thumbnail, and insert the database code. The following is a complete code for generating thumbnails and inserting data into the database when uploading images in PHP .? Php $ title $ _ POST [title]; $ descr resume ption $ _ POST [descr resume ption]; $ albumID $ _ POST [below is a complete code for PHP to generate a thumbnail and insert a database when writing and uploading images.
$ Title = $ _ POST [title];
$ Descr resume ption =$ _ POST [descr resume 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" ";
Echo "alert (the file type you uploaded is incorrect. The system only supports PNG, JPG, and GIF formats .); ";
Echo "location = ../uploadpicture. php? AlbumID = $ albumID ;";
Echo" ";
Exit;
}
$ Filename = date ("YmdHis"). substr (microtime (), 2, 5)... $ FileExtName;
$ PictureDir = substr (pathinfo ($ _ SERVER [scr limit pt _ FILENAME], PATHINFO_DIRNAME), 0, strlen (pathinfo ($ _ SERVER [scr limit pt _ FILENAME], PATHINFO_DIRNAME)-8 );
$ UploadURL = $ PictureDir./. $ PictureFolder. $ Filename;
$ ThumbsURL = $ PictureDir./. $ ThumbsFolder. $ Filename;
If (! Move_uploaded_file ($ _ FILES [url] [tmp_name], $ UploadURL )){
Echo" ";
Echo "alert (Upload failed. The image size cannot exceed $ MAX_FILE_SIZE KB .); ";
Echo "location = ../uploadpicture. php? AlbumID = $ albumID ;";
Echo" ";
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, $ 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 resume ption, uploadtime, updatetime, isvisible, iscommend) VALUES ($ albumID, $ title, $ Filename, $ descr resume 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" ";
Echo "alert (uploaded successfully .); ";
Echo "location = ../uploadpicture. php? AlbumID = $ albumID ;";
Echo" ";
}
?>
I have read the above code and hope you will get something.

Bytes. ? Php $ title = $ _ POST [title]; $ descr resume ption =$ _ POST [descr resume ption]; $ albumID =$ _ POST [albu...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.