How can one efficiently store images in a database while only saving the image links?

When storing images in a database, it is more efficient to save the image links rather than the actual image files. This reduces the size of the database and improves performance when retrieving data. To implement this, you can upload the image files to a server and store the file path or URL in the database.

// Upload image file to server
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);

// Save image link to database
$image_link = "http://example.com/" . $target_file;
$query = "INSERT INTO images (image_link) VALUES ('$image_link')";
mysqli_query($connection, $query);