mysql| binary
How to access the binary file, with the following code description
First create the test table TestTable
CREATE TABLE testtable (ID INT (5) Not NULL auto_increment PRIMARY key,filename CHAR (255), data Longblob);
Save a file in a table
<?php
mysql_connect ("localhost", "root", "password"); Connecting to a database
mysql_select_db ("database"); Selected database
$filename = ""//here fill in the binary file name
$data = Addslashes (Fread (fopen ($filename, "R"), FileSize ($filename));//Open file and normalize data into variable $data
$result =mysql_query ("INSERT into TestTable (filename,data) VALUES (' $filename ', ' $data ')")//data inserted into database test table
Mysql_close ();
?>
Retrieving files from a table
<?php
if ($id) {
mysql_connect ("localhost", "root", "password");
mysql_select_db ("database");
$filename = ""//here fill in the binary file name
$query = "Select data from testtable where Filename= $filename";
$result = mysql_query ($query);
$data = mysql_result ($result, 0, "data");
?>
Note here that PHP generally only supports files less than 2M, if you want to access more than 2M of files, it is necessary to enter the system settings.