Skip to main content

How to Upload multiple files into Database in PHP/MySQLi

Screenshot
Body

In this tutorial. I'm going to show you how to upload multiple files using PHP/MySQLi.

Creating our Database

First step is to create our database.
  1. Open phpMyAdmin.
  2. Click databases, create a database and name it as "upload".
  3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `photo` (
  2. `photoid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `location` VARCHAR(150) NOT NULL,
  4. PRIMARY KEY(`photoid`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
delete_multiple

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.

  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","upload");
  5. if (!$conn) {
  6. die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. ?>

Creating our Output Folder

Next step is to create our output folder. This will serve as storage of uploaded files. We name the folder upload.

index.php

We create our Upload Form and show the files that we have uploaded. In the case of this tutorial, I've shown files uploaded as images.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Uploading Multiple Files using PHP</title>
  5. </head>
  6. <body>
  7. <div style="height:50px;"></div>
  8. <div style="margin:auto; padding:auto; width:80%;">
  9. <span style="font-size:25px; color:blue"><center><strong>Uploading Multiple Files into MySQL Database using PHP/MySQLi</strong></center></span>
  10. <hr>
  11. <div style="height:20px;"></div>
  12. <form method="POST" action="upload.php" enctype="multipart/form-data">
  13. <input type="file" name="upload[]" multiple>
  14. <input type="submit" value="Upload">
  15. </form>
  16. </div>
  17. <div style="margin:auto; padding:auto; width:80%;">
  18. <h2>Output:</h2>
  19. <?php
  20. include('conn.php');
  21. $query=mysqli_query($conn,"select * from photo");
  22. while($row=mysqli_fetch_array($query)){
  23. ?>
  24. <img src="<?php echo $row['location']; ?>" height="150px;" width="150px;">
  25. <?php
  26. }
  27.  
  28. ?>
  29. </div>
  30. </body>
  31. </html>

upload.php

Lastly, we create the code in uploading multiple files to our database.

  1. <?php
  2. include('conn.php');
  3.  
  4. foreach ($_FILES['upload']['name'] as $key => $name){
  5.  
  6. $newFilename = time() . "_" . $name;
  7. move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/' . $newFilename);
  8. $location = 'upload/' . $newFilename;
  9.  
  10. mysqli_query($conn,"insert into photo (location) values ('$location')");
  11. }
  12. header('location:index.php');
  13. ?>

And that ends this tutorial. If you have any comments or questions, feel free to comment below or message me here at sourcecodester. Hope this helps.

Happy Coding :)

Comments

Submitted bysnehashis chowrangi (not verified)on Sun, 12/24/2017 - 01:13

insert into photo (location) values ('$location')"); in this code can u tell me (location) for which reason ? $location represents column name in photo database.
Submitted bynurhodelta_17on Tue, 12/26/2017 - 08:35

In reply to by snehashis chowrangi (not verified)

"location" is the column name in the photo database while "$location" is the value you insert in "location" column. This value is the link where your uploaded file is located.

Add new comment