diff --git a/public/index.php b/public/index.php index 32b4fbf..3ace71e 100644 --- a/public/index.php +++ b/public/index.php @@ -263,7 +263,7 @@ function api_upload() $uuid = uniqid(); - exec_db("INSERT INTO files (uuid, src) VALUES ('{$uuid}', '{$src}')"); + exec_db("INSERT INTO files (uuid, src) VALUES ('{$uuid}', '{$src}')"); echo_json([ "code" => empty($src) ? 1 : 0, @@ -289,7 +289,7 @@ function init() define("DATA_PATH", BASE_PATH . "/./data/"); inti_db(); - + empty($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI'] == "/" && $_SERVER['REQUEST_URI'] = "/index"; check_api(); @@ -297,10 +297,12 @@ function init() if (str_starts_with($_SERVER['REQUEST_URI'], "/file")) { $id = explode("/", $_SERVER['REQUEST_URI'])[2] ?? ""; - $results = query_db("SELECT * FROM files WHERE uuid='{$id}' LIMIT 1"); - $row = $results->fetchArray(); + $stmt = get_db()->prepare("SELECT * FROM files WHERE uuid=:uuid LIMIT 1"); + $stmt->bindParam(':uuid', $id); - if(empty($row['stc'])){ + $row = $stmt->fetch(); + + if (empty($row['stc'])) { echo_json([ "code" => 1, "msg" => "图片不存在", @@ -333,23 +335,22 @@ function show_page() } } -function exec_db($sql){ - $db = new SQLite3(DATA_PATH . 'data.db'); - // 创建表 +function get_db() +{ + $db = new PDO('sqlite:' . DATA_PATH . 'data.db'); + + return $db; +} + +function exec_db($sql) +{ + $db = get_db(); + $db->exec($sql); - $db->close(); } -function query_db($sql){ - $db = new SQLite3(DATA_PATH . 'data.db'); - // 创建表 - $results = $db->query($sql); - $db->close(); - - return $results; -} - -function inti_db(){ +function inti_db() +{ exec_db('CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY, uuid TEXT, src TEXT)'); }