How can a graphic be changed via a form in PHP?

To change a graphic via a form in PHP, you can create a form with an input field for the image file and a submit button. When the form is submitted, you can use PHP to move the uploaded image file to the desired location or replace the existing image file.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['image'])) {
        $targetDir = "uploads/";
        $targetFile = $targetDir . basename($_FILES['image']['name']);
        
        if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) {
            echo "Image uploaded successfully.";
        } else {
            echo "Failed to upload image.";
        }
    }
}
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" value="Upload Image">
</form>