小文件php+SQLite存儲方案
更新時間:2010年09月04日 00:14:59 作者:
在互聯(lián)網(wǎng)快速發(fā)展的背景下,網(wǎng)絡(luò)上的數(shù)據(jù)內(nèi)容呈幾何級的增長,而其中增長最快并且最容易給信息基礎(chǔ)架構(gòu)帶來挑戰(zhàn)的就是數(shù)目龐大的小文件(如圖片、電子圖書、郵件等)。
我們草根站長購買的虛擬主機(jī)往往都有文件數(shù)量限制,大量小文件占用大量資源,落伍精華區(qū)也有兄弟推薦豆瓣的解決方法,但是要有主機(jī)權(quán)限。只能另裝思路,采用php+SQLite解決問題,經(jīng)過我測試,切實(shí)可行,現(xiàn)在推薦給大家。
現(xiàn)在公開代碼:
創(chuàng)建數(shù)據(jù)庫文件:php1.php
$db = new SQLite3('mysqlitedb.db');
//獲取文件2進(jìn)制流
$filename = "http://www.dbjr.com.cn/logo.gif";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
//創(chuàng)建數(shù)據(jù)表
$db->exec('CREATE TABLE person (idnum TEXT,name TEXT,photo BLOB)');
$stmt = $db->prepare("INSERT INTO person VALUES ('41042119720101001X', '張三',?)");
$stmt->bindValue(1, $contents, SQLITE3_BLOB);
$stmt->execute();
讀數(shù)據(jù)文件:php2.php
<?php
$pdo = new SQLite3('mysqlitedb.db');
$results = $pdo->query('select * from person');
while ($row = $results->fetchArray()) {
ob_start();
header("Content-Type: image/jpg");
echo $row['photo'] ;
ob_end_flush();
}
?>
網(wǎng)頁引用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ANSYS教程</title>
</head>
<body>
<img src="http://www.dbjr.com.cn/info.php" width="22" height="30" />
</body>
</html>
現(xiàn)在公開代碼:
創(chuàng)建數(shù)據(jù)庫文件:php1.php
復(fù)制代碼 代碼如下:
$db = new SQLite3('mysqlitedb.db');
//獲取文件2進(jìn)制流
$filename = "http://www.dbjr.com.cn/logo.gif";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
//創(chuàng)建數(shù)據(jù)表
$db->exec('CREATE TABLE person (idnum TEXT,name TEXT,photo BLOB)');
$stmt = $db->prepare("INSERT INTO person VALUES ('41042119720101001X', '張三',?)");
$stmt->bindValue(1, $contents, SQLITE3_BLOB);
$stmt->execute();
讀數(shù)據(jù)文件:php2.php
復(fù)制代碼 代碼如下:
<?php
$pdo = new SQLite3('mysqlitedb.db');
$results = $pdo->query('select * from person');
while ($row = $results->fetchArray()) {
ob_start();
header("Content-Type: image/jpg");
echo $row['photo'] ;
ob_end_flush();
}
?>
網(wǎng)頁引用:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ANSYS教程</title>
</head>
<body>
<img src="http://www.dbjr.com.cn/info.php" width="22" height="30" />
</body>
</html>
相關(guān)文章
詳解PHP中的mb_detect_encoding函數(shù)使用方法
這篇文章主要介紹了詳解PHP中的mb_detect_encoding函數(shù)使用方法,包括對字符串編碼的轉(zhuǎn)換和判斷以及Call to undefined function mb_detect_encoding()錯誤的解決,需要的朋友可以參考下2015-08-08PHP GD 圖像處理組件的常用函數(shù)總結(jié)
如今,在互聯(lián)網(wǎng)上,很多網(wǎng)站都要處理大量圖片,比如:頭像、上傳的圖片做縮略圖加水印等等。他們需要服務(wù)端的大量圖片處理,一門好的服務(wù)端語言環(huán)境一定要有對圖像處理的支持。2010-04-04php中將一段數(shù)據(jù)存到一個txt文件中并顯示其內(nèi)容
這篇文章主要介紹了php中將一段數(shù)據(jù)存到一個txt文件中,并獲取其內(nèi)容顯示的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08Windows平臺實(shí)現(xiàn)PHP連接SQL Server2008的方法
這篇文章主要介紹了Windows平臺實(shí)現(xiàn)PHP連接SQL Server2008的方法,結(jié)合實(shí)例形式分析了Windows平臺PHP連接SQL Server2008所需的相關(guān)dll動態(tài)鏈接庫文件及相應(yīng)的配置與使用方法,需要的朋友可以參考下2017-07-07將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法
本篇文章是對IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07php調(diào)用KyotoTycoon簡單實(shí)例
這篇文章主要介紹了php調(diào)用KyotoTycoon簡單實(shí)例,涉及php調(diào)用KyotoTycoon的簡單使用方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04