38671-vm/api/photos.php
2026-02-21 16:43:49 +00:00

31 lines
1.1 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Handle photo upload
if (isset($_FILES['photo'])) {
$file = $_FILES['photo'];
$weight = $_POST['weight'] ?? null;
$date = $_POST['date'] ?? date('Y-m-d');
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = uniqid('photo_') . '.' . $ext;
$target = '../assets/images/progress/' . $filename;
if (move_uploaded_file($file['tmp_name'], $target)) {
$stmt = $pdo->prepare("INSERT INTO progress_photos (photo_path, weight, logged_at) VALUES (?, ?, ?)");
$stmt->execute(['assets/images/progress/' . $filename, $weight, $date]);
echo json_encode(['success' => true]);
} else {
echo json_encode(['error' => 'Failed to save file']);
}
exit;
}
}
// Default action: list photos
$stmt = $pdo->query("SELECT * FROM progress_photos ORDER BY logged_at DESC, created_at DESC");
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));