MySQL – Inserting and reading BLOB’s in PHP This paper provides the code needed to both write binary content to a MySQL table and to read the same data back. In our example, we will create two scripts, one that reads the contents of a PDF file and adds this into a table. The second script will read the contents of the same record in MySQL and output it to a file. We will then show that the two files are identical. The table that we created is as follows: CREATE TABLE testblob( id INT AUTO_INCREMENT , data MEDIUMBLOB, PRIMARY KEY ( id ) ) ENGINE = InnoDB; The first script reads the contents of a PDF file called itiss.pdf. At this point the binary has been added into the database. We now need a second script which will read this content and write it back out to a separate file: To see this in action: $ mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 64 Server version: 5.0.45 SUSE MySQL RPM Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(1) from testblob; +----------+ | count(1) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec) mysql> quit Bye $ php writeblob.php $ php readblob.php $ ls -ltr total 52 -rw-r--r-- 1 rcashell users 108 2011-01-10 09:40 blob.sql -rw-r--r-- 1 rcashell users 17453 2011-01-10 10:00 itiss.pdf -rw-r--r-- 1 rcashell users 298 2011-01-10 10:06 writeblob.php -rw-r--r-- 1 rcashell users 366 2011-01-10 10:06 readblob.php -rw-r--r-- 1 rcashell users 17453 2011-01-10 10:15 itiss1.pdf Recommend this post on Google