How to access the binary file, use the following code to demonstrate how to first create the Test table testtableCREATETABLEtesttable (idINT (5) NOTNULLAUTO_INCREMENTPRIMARYKEY, filenameCHAR (255), dataLONGBLOB); save the file into the table? Use the following code to explain how phpmysq can access a binary file: create a test table testtable.
Create table testtable (id INT (5) not null AUTO_INCREMENT primary key, filename CHAR (255), data LONGBLOB); save the file to the TABLE
Mysql_connect ('localhost', 'root', 'password'); // connect to the database
Mysql_select_db ('database'); // select a database
$ Filename = ''// enter the binary file name here.
$ Data = addslashes (fread (fopen ($ filename, 'r'), filesize ($ filename ))); // open the file and save the canonicalized data to the variable $ data $ result = mysql_query ('Insert INTO testtable (filename, data) VALUES ('$ filename',' $ data ') '); // Insert the data into mysql_close () in the database test table ();
?> Retrieve files from a table
If ($ id) {mysql_connect ('localhost', 'root', 'password'); mysql_select_db ('database ');
$ Filename = ''// enter the binary file name $ query = 'SELECT data from testtable where filename = $ filename ';
$ Result = mysql_query ($ query); $ data = mysql_result ($ result, 0, 'data');?> Note that PHP generally only supports files smaller than 2 MB. if you want to access files larger than 2 MB, you need to configure the system.
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