php blob text_PHP存储blob示例(转)

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

/*

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'];

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

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

C# Azure 存储-Blob

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

mysql存储blob限制

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

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

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

Kubernetes持久化存储1——示例

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

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

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

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

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

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

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

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

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

随机推荐

Divshot —— 在线的可视化网页设计

Divshot 是一个在线网站,通过可视化方式进行 Web 界面的设计,并直接生成 HTML 和 CSS 代码.该工具提供常用的 Web UI 组件.界面基于 Twitter 的 Bootstrap  ...

bugfree安装

1.下载xampp文件:xampp-linux-x64-5.5.30-3-installer.run 2.安装此文件,用root账号安装,安装命令:./xampp-linux-x64-5.5.30-3 ...

IIS mime类型 任意类型

HTTP头   任意mime类型   .*    application/octet-stream

js 下载文件 window.location.href

window.location.href ="../../pages2/assessmentplan/exportPointAsessment.do?planId="+planId ...

TimeDelta.total_seconds() in Python2.6-

Python 的日期操作真是无力吐槽. 如果在做日期相加减时使用TimeDelta对象,2.7及以后的TimeDelta有total_seconds()方法获取总秒数,而2.6之前没有该方法,且众所周 ...

linux出现tmp空间满的情况解决

cd命令tab补全的时候报错: cd /ro-bash: cannot create temp file for here-document: No space left on device-bash ...

insert into与insert ignore以及replace into的区别

insert ignore表示,如果表中已经存在相同的记录,则忽略当前新数据: INSERT INTO有无数据都插入,如果主键则不插入; REPLACE INTO 如果是主键插入则会替换以前的数据; ...

Kafka消息的可靠性测试--针对直播业务的方案选择

转自:http://blog.csdn.net/bailove/article/details/44240303 业务场景 来疯直播互动平台,每天有数百万人上下线,有数十万人同时参与互动直播聊天.用户 ...

docker-5-容器数据卷

1.是什么 一句话:有点类似我们Redis里面的rdb和aof文件   先来看看Docker的理念: *  将运用与运行的环境打包形成容器运行 ,运行可以伴随着容器,但是我们对数据的要求希望是持久化的 ...

程序运行bug查看

1.左击计算机进入管理,点击windows日志,查看程序信息. 可以方便看到报错信息.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值