PHP saves images in binary form to MySQL database _php tutorial

Source: Internet
Author: User
Tags fread mysql tutorial
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 ...

  • 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.