原文:http://www.mysqltutorial.org/php-mysql-blob/

<?php

/*
CREATE TABLE files (
id INT AUTO_INCREMENT PRIMARY KEY,
mime VARCHAR (255) NOT NULL,
data BLOB NOT NULL
);
*/ class BobDemo { const DB_HOST = 'localhost';
const DB_NAME = 'weiphp3';
const DB_USER = 'root';
const DB_PASSWORD = '123'; /**
* PDO instance
* @var PDO
*/
private $pdo = null; /**
* Open the database connection
*/
public function __construct() {
// open database connection
$conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try {
$this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);
//for prior PHP 5.3.6
//$conn->exec("set names utf8");
} catch (PDOException $e) {
echo $e->getMessage();
}
} /**
* insert blob into the files table
* @param string $filePath
* @param string $mime mimetype
* @return bool
*/
public function insertBlob($filePath, $mime) {
$blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
$stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime);
$stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute();
} /**
* update the files table with the new blob from the file specified
* by the filepath
* @param int $id
* @param string $filePath
* @param string $mime
* @return bool
*/
function updateBlob($id, $filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "UPDATE files
SET mime = :mime,
data = :data
WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime);
$stmt->bindParam(':data', $blob, PDO::PARAM_LOB);
$stmt->bindParam(':id', $id); return $stmt->execute();
} /**
* select data from the the files
* @param int $id
* @return array contains mime type and BLOB data
*/
public function selectBlob($id) { $sql = "SELECT mime,
data
FROM files
WHERE id = :id;"; $stmt = $this->pdo->prepare($sql);
$stmt->execute(array(":id" => $id));
$stmt->bindColumn(1, $mime);
$stmt->bindColumn(2, $data, PDO::PARAM_LOB); $stmt->fetch(PDO::FETCH_BOUND); return array("mime" => $mime,
"data" => $data);
} /**
* close the database connection
*/
public function __destruct() {
// close the database connection
$this->pdo = null;
} } $blobObj = new BobDemo(); // test insert gif image
$blobObj->insertBlob('images/php-mysql-blob.gif',"image/gif");
$a = $blobObj->selectBlob(1);
header("Content-Type:" . $a['mime']);
echo $a['data'];
// test insert pdf
//$blobObj->insertBlob('pdf/php-mysql-blob.pdf',"application/pdf");
//$a = $blobObj->selectBlob(2);
// save it to the pdf file
//file_put_contents("pdf/output.pdf", $a['data']);
// $a = $blobObj->selectBlob(2);
// header("Content-Type:" . $a['mime']);
// echo $a['data'];
// replace the PDF by gif file // $blobObj->updateBlob(2, 'images/php-mysql-blob.gif', "image/gif"); // $a = $blobObj->selectBlob(2);
// header("Content-Type:" . $a['mime']);
// echo $a['data'];

PHP存储blob示例(转)的更多相关文章

  1. Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例

    Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例(github代码:https://github.com/qq719862911/MemcacheTestDemo ...

  2. C# Azure 存储-Blob

    1. 前言 本文是根据Azure文档与本人做了验证之后写的. 如果想下载微软官网的demo, 请前往github https://github.com/Azure-Samples/storage-bl ...

  3. mysql存储blob限制

    一.Mysql存储类型分类: 1.blob:二进制大对象(字节流),可以用来存储图片.视频等,没有字符集的说法 2.text:文本大对象(字符流),存储大型字串,有字符集的说法 3.二者使用时不能指定 ...

  4. C和指针 3.9作用域、存储类型示例

    ; extern int b; static int c; int d( int e ) { ; register int b; ; extern int a; ... { int e; int a; ...

  5. Kubernetes持久化存储1——示例

    目录贴:Kubernetes学习系列 一.简介 存储管理与计算管理是两个不同的问题.Persistent Volume子系统,对存储的供应和使用做了抽象,以API形式提供给管理员和用户使用.要完成这一 ...

  6. 关于InnoDB存储引擎text和blob类型的优化

    我们在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,如果单表的存储空间达到了近上百G或者大几十G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们有 ...

  7. 【mysql】关于InnoDB存储引擎 text blob 大字段的存储和优化

    最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...

  8. [MySQL优化案例]系列 — 优化InnoDB表BLOB列的存储效率

    首先,介绍下关于InnoDB引擎存储格式的几个要点:1.InnoDB可以选择使用共享表空间或者是独立表空间方式,建议使用独立表空间,便于管理.维护.启用 innodb_file_per_table 选 ...

  9. 关于InnoDB存储引擎 text blob 大字段的存储和优化

    最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...

随机推荐

  1. ecmobile中IOS版本中界面文字不显示的解决

    查找以下方法. - (BOOL)setCurrentLanguageName:(NSString *)name{        BeeLanguage * lang = [self findLangu ...

  2. OS X Yosemite Beta体验

    自从看到Yosemite泄露图的那天起,就暗暗下决心,一定要坚守Mavericks阵营,坚决不升级,觉得新版系统界面简直其丑无比,结果过了没多久,就按耐不住了,在Windows下的虚拟机里面看了看,发 ...

  3. .NET 开发快捷键大全

    序号 快捷键 说明 1 Ctrl + Tab 切换当前编辑视图 2 Ctrl + Shift + Tab 反向切换当前编辑视图 3 F7 将窗体设计视图转成其代码视图 4 Shift + F7 将代码 ...

  4. php5.3到php7.0.x新特性介绍

    <?php /*php5.3*/ echo '<hr>'; const MYTT = 'aaa'; #print_r(get_defined_constants()); /* 5.4 ...

  5. PHP静态变量定义memcache对象的调用

    <?php $do = $_GET['do']; myCache($do,'do'); echo myCache($do); /** * 缓存管理 * @param mixed $name 缓存 ...

  6. IE8、IE9浏览器下报:JSON未定义 解决方法

    IE8.IE9浏览器下报:JSON未定义的问题 解决方法: 在jsp中引入如下代码 <!-- 解决 IE8.IE9 下显示混乱的问题--><% String browserStrin ...

  7. BigInger isProbablePrime

    JAVA BigInteger 成员函数: isProbablePrime public boolean isProbablePrime(int certainty) 如果此 BigInteger 可 ...

  8. 转 Dynamics CRM Alert and Notification JavaScript Methods

    http://www.powerobjects.com/2015/09/23/dynamics-crm-alert-and-notification-javascript-methods/ Befor ...

  9. oracle修改字符集

    在linux下面安装了oracle 11G,由于安装的时候没有注意导致字符集问题,impdp导入操作后中文乱码. 1.用命令select userenv('language') from dual; ...

  10. Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询

    最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo. 使用的lucene jar包是3.6版本. 一:建立数据库表,并加上测试数据.数据库表:UserInf ...