Home > php教程 > php手册 > body text

MySQL中如何存取二进制文件

WBOY
Release: 2016-06-13 11:21:40
Original
938 people have browsed it

如何存取二进制文件,用以下代码说明

首先创建测试表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的文件,那就要进系统方面的设置了。


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template