How to upload an image to a database with PHP and MySQL

<?php
//connect to database
$conn = mysqli_connect(“localhost”, “username”, “password”, “database_name”);

//check if form is submitted
if(isset($_POST[‘submit’])){
$image = $_FILES[‘image’][‘name’];
$tmp_image = $_FILES[‘image’][‘tmp_name’];

//move uploaded image to a folder
move_uploaded_file($tmp_image, “upload/”.$image);

//insert image into database
mysqli_query($conn, “INSERT INTO images (image) VALUES (‘$image’)”);
}

//display uploaded images
$result = mysqli_query($conn, “SELECT * FROM images”);
while($row = mysqli_fetch_array($result)){
$image = $row[‘image’];
echo ““;
}
?>

 Note: This code is just an example and should not be used in production without proper security measures.

Learn more about uploading files with PHP and MySQL

  Written by: Steven Iversen     19-06-2023     Written in: Uncategorized

Leave a Reply