Use PDO storage to store pictures and audio files in a database

Source: Internet
Author: User
Tags getmessage prepare

When creating a table in the database, there is a field image, which is used to save the picture, then its type is blob, about the Blob, Baidu Encyclopedia is described this way

BLOB (Binary large object), binary large, is a container that can store binary files. In computers, blobs are often the type of field used in a database to store binary files. A blob is a large file, a typical blob is a picture or a sound file, and due to its size, it must be handled in a special way (for example, uploading, downloading, or storing to a database). According to Eric Raymond, the main idea of dealing with blobs is to let a file processor (such as a database manager) ignore what the file is, but rather care about how to handle it. But there are also experts who stress that the way to handle big data objects is to double-edged swords, which may cause problems such as storing binary files too large, which can degrade the performance of the database. Storing large multimedia objects in a database is a typical example of application processing blobs. The principle of storing files such as pictures or audio in a database is particularly well understood because they are files that can be read with a function and then saved in a string and then stored in a database. The database is as follows:
1 CREATE TABLE pic (2       Key null,3       null4 ) Engine=myisam Charset=utf8;

Create a form to submit a file, note the form that submits the file (the form's Properties method= "POST" enctype= "Multipart/form-data")

1<form action= "" method= "Post" enctype= "Multipart/form-data" >2<input type= "file" name= "pic" >3<input type= "Submit" Name= "Submit" >4</form>5 6<?PHP7     if(!Empty($_post)){8         Try {9             $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "root", "root");Ten             $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception); One              A             $statement=$pdo->prepare ("INSERT INTO pic values (: id,:image)"); -  -             //Start reading Files the             $fp=fopen($_files[' Pic '] [' Tmp_name '], "RB"); -             $content=""; -             $content=fread($fp,filesize($_files[' Pic '] [' Tmp_name '])); -             fclose($fp); +  -             $statement->execute (["id" =>1, "image" + =$content]); +             Echo"The picture has been saved\n"; A}Catch(pdoexception$e) { at             Echo"Failed to save the picture"; -             Echo $e-getMessage (); -         } -      } -?>

You can output content in the middle of the contents, or after the completion of the database to see the added content, but you do not understand what the content is all garbled, but you successfully saved the picture to the database.

Next, you can put the picture "data" in the database, read it, and then display it in the form of a picture, to see whether the picture has been submitted, the code is as follows:

1<?PHP2     Try {3             $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "root", "root");4             $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);5             $statement=$pdo->prepare ("SELECT * from pic");6             $statement-execute ();7             List($id,$image)=$statement->fetch (PDO::fetch_num);8             Header("Content-type:image/png");9             Echo $image;Ten}Catch(pdoexception$e) { One             Echo"Failed to open the picture"; A             Echo $e-getMessage (); -         } -?>

Use PDO storage to store pictures and audio files in a database

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.