Home  >  Article  >  Backend Development  >  How to access binary files in MySQL_PHP tutorial

How to access binary files in MySQL_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:05:401015browse

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445113.htmlTechArticleHow to access binary files, use the following code to illustrate how to first create a test table testtable CREATE TABLE testtable ( id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,filename CHAR(255),data LO...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn