<?php
require 'functions.php';

$index = $_POST['index'];
$data = loadData();

if (!empty($_FILES['image']['name'])) {
    $old = 'uploads/' . $data[$index]['filename'];
    if (file_exists($old)) unlink($old);

    $filename = uniqid() . '_' . basename($_FILES['image']['name']);
    move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $filename);
    $data[$index]['filename'] = $filename;
    saveData($data);
}

header('Location: index.php');
?>
