What are the best practices for automatically generating and storing image links in a database using PHP?

When automatically generating and storing image links in a database using PHP, it is important to ensure that the links are properly formatted and stored securely to prevent any vulnerabilities. One way to achieve this is by using a combination of unique identifiers and file extensions to create the image links, and then storing these links in the database along with other relevant information.

// Generate a unique identifier for the image link
$imageId = uniqid();

// Get the file extension of the uploaded image
$imageExtension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);

// Construct the image link using the unique identifier and file extension
$imageLink = "images/" . $imageId . "." . $imageExtension;

// Move the uploaded image to the desired directory
move_uploaded_file($_FILES['image']['tmp_name'], $imageLink);

// Store the image link in the database along with other relevant information
$query = "INSERT INTO images (image_link, title, description) VALUES ('$imageLink', '$title', '$description')";
$result = mysqli_query($connection, $query);

if($result) {
    echo "Image link stored successfully!";
} else {
    echo "Error storing image link!";
}