1.选择图片&上传
2.图片链接
名称 大小 状态 链接
prepare("SELECT count(1) as count FROM files"); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); ob_start(); ?>

这是一个简单图床.


目前共成功上传了: 张图片.

404 NOT FOUND
1, "msg" => "uri错误", "data" => [] ]); call_user_func($func); } } function echo_json($data) { header('Content-Type: application/json; charset=utf-8'); echo json_encode($data, JSON_UNESCAPED_UNICODE); die; } function api_test() { echo_json([ "code" => 0, "msg" => "test", "data" => [] ]); } function api_upload() { $web = get_config('common', 'web'); $url = "https://telegra.ph/upload"; $filePath = $_FILES["file"]["tmp_name"]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'file' => new CURLFile($filePath) )); $response = curl_exec($ch); curl_close($ch); $jsonArr = json_decode($response, true); $src = $jsonArr[0]["src"] ?? ""; $uuid = uniqidReal(); exec_db("INSERT INTO files (uuid, src) VALUES ('{$uuid}', '{$src}')"); echo_json([ "code" => empty($src) ? 1 : 0, "msg" => "", "data" => [ "id" => $_POST["id"], "src" => $web . "/file/{$uuid}" ] ]); } // api func end ?> exec($sql); } function inti_db() { exec_db('CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY, uuid TEXT, src TEXT)'); } function uniqidReal($lenght = 13) { // uniqid gives 13 chars, but you could adjust it to your needs. if (function_exists("random_bytes")) { $bytes = random_bytes(ceil($lenght / 2)); } elseif (function_exists("openssl_random_pseudo_bytes")) { $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2)); } else { throw new Exception("no cryptographically secure random function available"); } return substr(bin2hex($bytes), 0, $lenght); } function check_file() { if (str_starts_with($_SERVER['REQUEST_URI'], "/file")) { $id = explode("/", $_SERVER['REQUEST_URI'])[2] ?? ""; $stmt = get_db()->prepare("SELECT * FROM files WHERE uuid=:uuid LIMIT 1"); $stmt->bindParam(':uuid', $id); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (empty($row['src'])) { echo_json([ "code" => 1, "msg" => "图片不存在", "data" => [] ]); } $imgUrl = "https://telegra.ph{$row['src']}"; $content = file_get_contents($imgUrl); header('Content-Type: image/jpeg'); header('Content-Length: ' . strlen($content)); echo $content; die; } } function get_config($file, $path) { if (!file_exists(CONFIG_PATH . $file . ".json")) { return ""; } $conf = file_get_contents(CONFIG_PATH . $file . ".json"); $arr = json_decode($conf, true); $value = $arr; foreach (explode(".", $path) as $p) { $value = $value[$p] ?? ""; if (!$value) break; } return $value; } // common func end ?>