Php saves the image in binary format to mysql database _ PHP Tutorial

Source: Internet
Author: User
Php saves the image to the mysql database in binary format. Php saves the image in binary format to mysql database php Tutorial saves the image in binary format to mysql tutorial database tutorial $ errmsg; if (! @ Mysql_connect (localhost, train php saves the image to the mysql database in binary format

Php Tutorial save the image in binary format to mysql tutorial database tutorial

$ Errmsg = "";
If (! @ Mysql_connect ("localhost", "trainee", "abc123 ")){
$ Errmsg = "Cannot connect to database ";
}
@ Mysql_select_db ("wellho ");

// First run ONLY-need to create table by un commenting this
// Or with silent @ we can let it fail every subsequent time ;-)

$ Q = < Create table pix (
Pid int primary key not null auto_increment,
Title text,
Imgdata longblob)
CREATE;
@ Mysql_query ($ q );

// Insert any new image into database

If ($ _ REQUEST [completed] = 1 ){
// Need to add-check for large upload. Otherwise the code
// Will just duplicate old file ;-)
// ALSO-note that latest. img must be public write and in
// Live application shoshould be in another (safe !) Directory.
Move_uploaded_file ($ _ FILES ['imagefile'] ['tmp _ name'], "latest. img ");
$ Instr = fopen ("latest. img", "rb ");
$ Image = mysql_real_escape_string (fread ($ instr, filesize ("latest. img ")));
If (strlen ($ instr) <149000 ){
Mysql_query ("insert into pix (title, imgdata) values ("".
$ _ REQUEST [whatsit].
"","".
$ Image.
"")");
} Else {
$ Errmsg = "Too large! ";
}
}

// Find out about latest image

$ Gotten = @ mysql_query ("select * from pix order by pid desc limit 1 ");
If ($ row = @ mysql_fetch_assoc ($ gotten )){
$ Title = htmlspecialchars ($ row [title]);
$ Bytes = $ row [imgdata];
} Else {
$ Errmsg = "There is no image in the database yet ";
$ Title = "no database image available ";
// Put up a picture of our training center
$ Instr = fopen ("../wellimg/ctco.jpg", "rb ");
$ Bytes = fread ($ instr, filesize ("../wellimg/ctco.jpg "));
}

// If this is the image request, send out the image

If ($ _ REQUEST [gim] = 1 ){
Header ("Content-type: image/jpeg ");
Print $ bytes;
Exit ();
}
?>


Upload an image to a database
Here's the latest picture





Please upload a new picture and title



Link-view images already uploaded

Link-technical article including source code


Note
-By uploading an image to this site, you agree that you are
Copyright owner, that the picture is legally acceptable and that you take
Full responsibility for this, and that the owners of this site are
Henceforth able to make free limitless use of the picture if they so
Wish. Your IP address and other details may be logged as you upload.
Sorry about this note-have to protect ourselves.

By Graham Ellis-graham@wellho.net


Http://www.bkjia.com/PHPjc/630872.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630872.htmlTechArticlephp save the image in binary form to mysql database php Tutorial save the image in binary form to mysql tutorial database tutorial $ errmsg =; if (! @ Mysql_connect (localhost, train...

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.