<?php
include "../../config/master.php";

$id=$_POST['id'];
$title=$_POST['title'];

$data=mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM gallery_images WHERE id='$id'"));
$image=$data['image'];

if(isset($_FILES['image']) && $_FILES['image']['error']==0){

$dir="../../uploads/gallery/";

$ext=pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);

$image="gallery_".time().rand(100,999).".".$ext;

move_uploaded_file($_FILES['image']['tmp_name'],$dir.$image);

if(!empty($data['image'])){
$file=$dir.$data['image'];
if(file_exists($file)) unlink($file);
}
}

mysqli_query($conn,"
UPDATE gallery_images SET
title='$title',
image='$image'
WHERE id='$id'
");

echo json_encode(["status"=>"success"]);