Php stores images in binary format into the mysql database and displays the implementation code _ PHP Tutorial

Source: Internet
Author: User
Php stores the image in binary format into the mysql database and displays the implementation code. Copy the code of the php code that saves the image to the database: If ($ Picture! None) {$ PSizefilesize ($ Picture); $ mysqlPictureaddslashes (fread (fopen ($ Picture, r), $ PSize // php code for saving images to the database

The code is as follows:


If ($ Picture! = "None "){
$ PSize = filesize ($ Picture );
$ MysqlPicture = addslashes (fread (fopen ($ Picture, "r"), $ PSize ));
Mysql_connect ($ host, $ username, $ password) or die ("Unable to connect to SQL server ");
@ Mysql_select_db ($ db) or die ("Unable to select database ");
Mysql_query ("insert into Images (Image) VALUES ($ mysqlPicture)") or die ("Cant Perform Query ");
} Else {
Echo "You did not upload any picture ";
}


// Code for reading images in the database using the img tag

The code is as follows:


Mysql_connect ($ host, $ username, $ password) or die ("Unable to connect to SQL server ");
@ Mysql_select_db ($ db) or die ("Unable to select database ");
$ Result = mysql_query ("SELECT * FROM Images") or die ("Cant Perform Query ");
While ($ row = mysql_fetch_object ($ result )){
Echo "PicNum"> ";

// The code for the secoed. php file is as follows:
$ Result = mysql_query ("SELECT * FROM Images WHERE PicNum = $ PicNum") or die ("Cant perform Query ");
$ Row = mysql_fetch_object ($ result );
Header ("Content-type: image/gif ");
Echo $ row-> Image;

The example code is as follows: If ($ Picture! = "None") {$ PSize = filesize ($ Picture); $ mysqlPicture = addslashes (fread (fopen ($ Picture, "r"), $ PSize...

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.