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>