PHP saves images in binary form to MySQL database
PHP Tutorial to save images in binary form 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
Would just duplicate old file;-)
Also-note that latest.img must is public write and in a
Live application should is 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 centre
$instr = fopen (".. /wellimg/ctco.jpg "," RB ");
$bytes = Fread ($instr, FileSize (". /wellimg/ctco.jpg "));
}
If 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
Upload a new picture and title
Link-view Images already uploaded
Link-technical article including source code
Note-by uploading a image to this site, you agree the
Copyright owner, that's picture was legally acceptable and that's take
Full responsibility for this, and that the owners of this site is
Henceforth able to make free limitless use of the picture if they so
Wish. Your IP Address and other details logged as you upload.
Sorry about the Note-have to protect ourselves.
by Graham Ellis-graham@wellho.net
http://www.bkjia.com/PHPjc/630872.html www.bkjia.com true http://www.bkjia.com/PHPjc/630872.html techarticle PHP saves the picture in binary form to the MySQL database PHP tutorial to save the picture in binary form to the MySQL tutorial database tutorial $errmsg =; if (! @ mysql_connect (localhost, train ...