小文件php+SQLite存储方案

yipeiwu_com6年前PHP代码库
我们草根站长购买的虚拟主机往往都有文件数量限制,大量小文件占用大量资源,落伍精华区也有兄弟推荐豆瓣的解决方法,但是要有主机权限。只能另装思路,采用php+SQLite解决问题,经过我测试,切实可行,现在推荐给大家。

现在公开代码:
创建数据库文件:php1.php
复制代码 代码如下:

$db = new SQLite3('mysqlitedb.db');

//获取文件2进制流
$filename = "//www.jb51.net/logo.gif";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
//创建数据表
$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();

读数据文件: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();
}
?>

网页引用:
复制代码 代码如下:

<!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="//www.jb51.net/info.php" width="22" height="30" />
</body>
</html>

相关文章

PHP中if和or运行效率对比

本文实例讲述了PHP中if和or运行效率对比。分享给大家供大家参考。具体实现方法如下: 对if和or的运行效率进行了实例说明,感兴趣的朋友可以测试一下,这里我测试了的结果是or 比if效...

有关phpmailer的详细介绍及使用方法

第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket ,通过phpi...

实例讲解PHP面向对象之多态

什么是多态性? 多态性是继数据库抽象和继承后,面向对象语言的第三个特征。多态即多种形态,具有表现多种形态的能力特征。在面向对象中表示根据对象的类型以不同方式处理。多态性允许每个对象以适合...

详解PHP中mb_strpos的使用

mb_strpos (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_strpos — Find position of first occurrence of...

解析PHP正则提取或替换img标记属性

核心代码 <?php /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img src="/uploads/imag...