In this tutorial, we'll talk about how to upload images in PHP and store them in a folder and database. PHP is a server-side application on a creates a dynamic website. PHP handles the uploading of image files to store the server and display them on the website. This Website form should contain the attributes as method=’post’ and enctype=’multipart/form-data to support file upload. Follows YouTube tutorial(CodeAT21) and GitHub repository full source code Available.

Upload Multiple Images And Store In Database Using PHP And MySQL.

Next, Project structure your folder as follows.

├── css
|   └── style.css
├── uploads
├── dbConfig.php
├── index.php
└── upload.php 

MySQL database

Create a database called codeat21 and create a table called images with fields:

  • Database Name – codeat21
  • Table Name – images
CREATE TABLE `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `file_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `uploaded_on` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
<?php // Database configuration $dbHost = "localhost"; $dbUsername = "root"; $dbPassword = ""; $dbName = "codeat21"; // Create database connection $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } ?>

Create a Form Using HTML for Uploading the Image Files.

The following HTML code will create a simple form with a post method and save it an index.php file name. This code with a “choose file” option and an upload-to button.

<!--File uploading script --> <?php include 'upload.php'; ?> <!DOCTYPE html> <html lang="en-US"> <head> <title>Upload Image and Store in the Database by CodeAT21</title> <meta charset="utf-8"> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="App"> <h1> Upload Images And Store In Database Using PHP And MySQL </h1> <div class="wrapper"> <div class="form__field"> <?php if(!empty($statusMsg)){ ?> <p class="status__msg"><?php echo $statusMsg; ?></p> <?php } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file" class="form__field__img"> <input type="submit" name="submit" value="Upload" class="btn__default"> </form> </div> <div class="gallery"> <h2>Uploaded Images</h2> <?php include 'dbConfig.php'; $query = $db->query("SELECT * FROM images ORDER BY uploaded_on DESC"); if($query->num_rows > 0){ while($row = $query->fetch_assoc()){ $imageURL = 'uploads/'.$row["file_name"]; ?> <img src="<?php echo $imageURL; ?>" alt="" /> <?php } }else{ ?> <p>No image(s) found...</p> <?php } ?> </div> </div> </div> </body> </html>

The <form> tag attributes are:

  • “upload.php” – file upload on the server
  • method=”post” – sending the file to the server
  • enctype=”multipart/form-data” – content-type

Upload File to Server and Insert the Image File in Database.

The following PHP code will connect the MySQL database server to upload an image file and insert it into the database.

<?php include 'dbConfig.php'; $statusMsg = ''; // File upload directory $targetDir = "uploads/"; if(isset($_POST["submit"])){ if(!empty($_FILES["file"]["name"])){ $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); // Valid file extensions $allowTypes = array('jpg','png','jpeg','gif'); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ $insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())"); if($insert){ $statusMsg = "The image file ".$fileName. " has been uploaded successfully."; }else{ $statusMsg = "Failed to upload image"; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } } ?>
  • $_FILES[“photo”][“name”] — the original name of the file, including the file extension. It doesn’t include the file path.
  • $_FILES[“photo”][“type”] — MIME type of the file.
  • $_FILES[“photo”][“size”] — size of the file (in bytes).
  • $_FILES[“photo”][“tmp_name”] — the temporary file name including full path and file stored on the server.
  • $_FILES[“photo”][“error”] — error or status code of the file.

Related keywords

  • How to upload image in php and store in folder and database
  • How to insert image in mysql database using php source code
  • Image upload in PHP code with databases
  • Upload image in php mysql database and display
  • PHP upload image and preview display instantly
  • PHP code for image upload and display
  • How to display image in php
  • How to display image in php from database
  • Upload Image to Database and Server using HTML,PHP
  • PHP image upload
  • PHP image upload and preview
  • Image store in mysql database
  • This website show country are united states, France, Canada, japan, New york, london, Los Angeles, Russia, Italy, Spain, Austria, Serbia, Moldova, Bosnia and Herzegovina, Albania, Lithuania, North Macedonia, Switzerland, Bulgaria, Denmark,Germany, United Kingdom.