PHP saves the uploaded map to the database and displays the code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags code connect create displays image mysql mysql tutorial mysql_query

PHP Tutorial Saves the uploaded map to the database tutorial and displays the code
Connect to Database

$errmsg = "";
if (! @mysql tutorial _connect ("localhost", "root", "")) {
$errmsg = "Cant 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

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
 

$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 verb";
   $title = "No database image available";
  //Put up a picture of our training Pew
   $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) {
&NBSP;&NB Sp; Header ("Content-type:image/jpeg");
   print $bytes;
   exit ();
  }

 
<html><head>
<title>upload an image to a database</title>
<body Bgcolor=white><h2>here ' s The latest picture</h2>
<font color=red><?= $errmsg? ></ Font>
<center><img src= width=144><br>
<b><?= $title?></center>
<hr>
<h2>please Upload a new picture and title</h2>
<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 a image to upload: <input type= "file" Name= "ImageFile" ><br>
Please enter the title of this 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.