Php saves the uploaded graph to the database and displays the code.

Source: Internet
Author: User
Tags fread mysql tutorial

// Connect to database
 

The code is as follows: Copy code
$ Errmsg = "";
If (! @ Mysql tutorial _ connect ("localhost", "root ","")){
$ Errmsg = "Cannot connect to database ";
   }
@ Mysql_select_db ("db1 ");
 
$ Q = <CREATE
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
 

The code is as follows: Copy code
If ($ _ REQUEST [completed] = 1 ){
Move_uploaded_file ($ _ FILES ['imagefile'] ['tmp _ name'], "latest. img ");
$ Instr = fopen ("latest. img", "rb ");
$ Image = addslashes (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
 

The code is as follows: Copy code
$ 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 ();
   }
?>

The code is as follows: Copy code
 
<Html> <Title> Upload an image to a database </title>
<Body bgcolor = white> <Font color = red> <? = $ Errmsg?> </Font>
<Center> <br>
<B> <? = $ Title?> </Center>
<Hr>
<H2> Please upload a new picture and title <Form enctype = "multipart/form-data" method = "post">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = 150000>
<Input type = "hidden" name = "completed" value = 1>
Please choose an image to upload: <input type = "file" name = "imagefile"> <br>
Please enter the title of that picture: <input name = "whatsit"> <br>
Then: <input type = "submit"> </form> <br>
 


</Body>
</Html>

Related Article

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.