Home >Backend Development >PHP Tutorial >How to access binary files in MySQL_PHP tutorial
How to access binary files, use the following code to explain
First create the test table testtable
CREATE TABLE testtable (id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,filename CHAR(255),data LONGBLOB );
Save the file into the table
mysql_connect( "localhost", "root", "password"); //Connect to the database
mysql_select_db( "database "); //Selected database
$filename="" //Fill in the binary file name here
$data = addslashes(fread(fopen($filename, "r"), filesize($filename)) );//Open the file and store the normalized data in the variable $data
$result=mysql_query( "INSERT INTO testtable (filename,data) VALUES ('$filename','$data')"); //Insert data into the database test table
mysql_close();
?>
Retrieve files from the table
if($id) {
mysql_connect( "localhost", "root", "password");
mysql_select_db( "database");
$filename=" " //Fill in the binary file name here
$query = "select data from testtable where filename=$filename";
$result = mysql_query($query);
$data = mysql_result($result,0, "data");
?>
It should be noted here that PHP generally only supports files smaller than 2M. If you want to access files larger than 2M file, then you need to enter the system settings.