Home >Backend Development >PHP Tutorial >Small file php+SQLite storage solution
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();
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();
}
?>
Copy code The code is as follows:
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.