原文: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. jQuery的.html(),.text()和.val()的概述及使用

    本节内容主要介绍的是如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法.jQuery中为我 ...

  2. 如何区分Babel中的stage-0,stage-1,stage-2以及stage-3(二)

    上一篇文章我们介绍了法力无边的stage-0 和 包罗万象的stage-1, 现在我们来介绍下 stage-2 和 stage-3 深藏不露的stage-2 为什么说 stage-2深藏不露呢,因为它 ...

  3. LeetCode "Wiggle Subsequence" !

    Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...

  4. day1作业--登录入口

    作业概述: 编写一个登录入口,实现如下功能: (1)输入用户名和密码 (2)认证成功后显示欢迎信息 (3)输错三次后锁定 流程图: readme: 1.程序配置文件: 黑名单文件blacklist.t ...

  5. linux新增用户并增加sudo权限

    创建用户.设置密码: useradd testuser 创建用户testuserpasswd testuser 给已创建的用户testuser设置密码 增加sudo权限: #vi /etc/sudoe ...

  6. 擦掉STM32F429芯片上的数据的一个方法

    刚入手一块STM32F429Discovery.手痒痒的,准备写个程序进去.一不小心,把MCU的调试接口SW.JTAG全部给禁用了.这下可坏了,写不进去程序,擦不掉数据.愁的某家一头大汗.突然想起了当 ...

  7. WAMP虚拟目录的设置

    1.打开Apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#!! 2.打开Apache的apache/conf/extr ...

  8. opengl入门学习

    OpenGL入门学习 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640 ...

  9. BestCoder Round #90 A.Kblack loves flag(随机数生成种子)

    A.Kblack loves flag [题目链接]A.Kblack loves flag [题目类型]水题 &题意: kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜. 某天,k ...

  10. 爬虫之scrapy框架

    解析 Scrapy解释 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓 ...