php mysql blob 存储_PHP存储blob示例(转)

id INT AUTO_INCREMENT PRIMARY KEY,

mime VARCHAR (255) NOT NULL,

data BLOB NOT NULL

);*/

classBobDemo {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 = newBobDemo();//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'];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值