Home  >  Article  >  Database  >  MYSQL中如何存取二进制文件_MySQL

MYSQL中如何存取二进制文件_MySQL

WBOY
WBOYOriginal
2016-06-01 14:04:58846browse
如何存取二进制文件,用以下代码说明

 

首先创建测试表testtable
CREATE TABLE testtable ( id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,filename CHAR(255),data LONGBLOB );

将文件存入表中
mysql_connect( "localhost", "root", "password"); //连接数据库
mysql_select_db( "database"); //选定数据库
$filename="" //这里填入二进制文件名
$data = addslashes(fread(fopen($filename, "r"), filesize($filename)));//打开文件并规范化数据存入变量$data中

$result=mysql_query( "INSERT INTO testtable (filename,data) VALUES ('$filename','$data')");//数据插入到数据库test表中

mysql_close();
?>

从表中取回文件


if($id) {

mysql_connect( "localhost", "root", "password");

mysql_select_db( "database");
$filename="" //这里填入二进制文件名

$query = "select data from testtable where filename=$filename";
$result = mysql_query($query);

$data = mysql_result($result,0, "data");

?>

这里要注意的是,PHP一般只支持小于2M的文件,如果要存取大于2M的文件,那就要进系统方面的设置了。

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