Home >Backend Development >PHP Tutorial >Small file php+SQLite storage solution

Small file php+SQLite storage solution

WBOY
WBOYOriginal
2016-07-29 08:43:43976browse

The virtual host purchased by our grassroots webmasters often has a limit on the number of files, and a large number of small files occupy a lot of resources. Some brothers in the outdated essence area also recommended Douban's solution, but they must have host permissions. I can only install another idea and use php+SQLite to solve the problem. After I tested it, it is feasible and I recommend it to everyone now.
Now public code:
Create database file: php1.php

Copy code The code is as follows:


$db = new SQLite3('mysqlitedb.db');
//Get the file binary stream
$filename = "http://www.jb51.net/logo.gif";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
//Create data table
$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();


Read data file :php2.php

Copy code The code is as follows:


$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();
}
?>


Webpage reference:

Copy code The code is as follows:






ANSYS Tutorial





The above has introduced the small file php+SQLite storage solution, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn