Upload Multiple Files in PHP /MYSQL

0
135

Good day everyone! This tutorial is all about Upload Multiple Files in PHP /MYSQL. Today I’m gonna teach you on “UPLOAD MULTIPLE FILES IN PHP/MYSQL”. This tutorial is very easy to understand. Now we will create a upload form with the maximum of three files to be uploaded.

So let’s start our tutorial now.

First, create a database then name it as any name you desire. In may case, I choose “itsourcecode” as the name of the database.

Then create a table then name it as “files” then put the following attributes.

  1. CREATE TABLE `blogs` ( `id` int(11) NOT NULL, `title` text NOT NULL, `slug` varchar(255) NOT NULL, `body` text NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now, create a connection to the database. Create a “conn.php” file then put the following codes.

  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'itsourcecode');
  3.  
  4. if ($conn->connect_error) {
  5. echo $conn->error;
  6. }
  7. ?>

On the index file. Put the following codes.

  1. <?php
  2. include 'conn.php';
  3. ?>
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Upload Multiple Files</title>
  5. <link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css"/>
  6. <style>
  7. body {
  8. padding-top: 10px;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="container">
  14. <div class="jumbotron">
  1. File/s Uploaded!
  2.  
  3. </div>
  4. ';
  5. }
  6. }
  7. ?&gt;
  1. &lt;h3&gt;Upload files:&lt;/h3&gt;
  2. &lt;form method="post" action="upload-action.php" enctype="multipart/form-data" role="form"&gt;
  3. &lt;input type="file" name="file1" /&gt;&lt;br&gt;
  4. &lt;input type="file" name="file2" /&gt;&lt;br&gt;
  5. &lt;input type="file" name="file3" /&gt;&lt;br&gt;
  6. <div class="btn-group">Reset
  7. <i class="btn btn-primary"></i></div>
  8. &lt;/form&gt;
  9. &lt;/div&gt;
  1. &lt;?php
  2. $files = $conn-&gt;query("SELECT * FROM files");
  3. ?&gt;
  1. &lt;/div&gt;
  2. &lt;/body&gt;
  3. &lt;/html&gt;

For the upload file action. Create a “upload-action.php” file then put the following codes.

  1. &lt;?php
  2. include 'conn.php';
  3. if (isset($_POST['upload'])) {
  4. if (isset($_FILES['file1']) &amp;&amp; $_FILES['file1']['name'] != null) {
  5. $file1_name = addslashes($_FILES['file1']['name']);
  6.  
  7. move_uploaded_file($_FILES['file1']['tmp_name'], 'files/'. $_FILES['file1']['name']);
  8. $filelink1 = "files/". $_FILES['file1']['name'];
  9.  
  10. $upload_file_1 = $conn-&gt;query("INSERT INTO files (file_name, file_link) VALUES ('$file1_name', '$filelink1')");
  11.  
  12. if ($upload_file_1) {
  13. header("Location: index.php?upload_action=uploaded_successful");
  14. } else {
  15. echo $conn-&gt;error;
  16. }
  17. }
  18. if (isset($_FILES['file2']) &amp;&amp; $_FILES['file2']['name'] != null) {
  19. $file2_name = addslashes($_FILES['file2']['name']);
  20.  
  21. move_uploaded_file($_FILES['file2']['tmp_name'], 'files/'. $_FILES['file2']['name']);
  22. $filelink2 = "files/". $_FILES['file2']['name'];
  23.  
  24. $upload_file_2 = $conn-&gt;query("INSERT INTO files (file_name, file_link) VALUES ('$file2_name', '$filelink2')");
  25.  
  26. if ($upload_file_2) {
  27. header("Location: index.php?upload_action=uploaded_successful");
  28. } else {
  29. echo $conn-&gt;error;
  30. }
  31. }
  32. if (isset($_FILES['file3']) &amp;&amp; $_FILES['file3']['name'] != null) {
  33. $file3_name = addslashes($_FILES['file3']['name']);
  34.  
  35. move_uploaded_file($_FILES['file3']['tmp_name'], 'files/'. $_FILES['file3']['name']);
  36. $filelink3 = "files/". $_FILES['file3']['name'];
  37.  
  38. $upload_file_3 = $conn-&gt;query("INSERT INTO files (file_name, file_link) VALUES ('$file3_name', '$filelink3')");
  39.  
  40. if ($upload_file_3) {
  41. header("Location: index.php?upload_action=uploaded_successful");
  42. } else {
  43. echo $conn-&gt;error;
  44. }
  45. }
  46. }
  47. ?&gt;

The 3 conditions refers to the 3 file inputs on the upload form. You can upload from 1 to 3 files on the upload form.

End.

Screenshots:

If you have questions regarding this tutorial entitled as “Upload Multiple Files in PHP /MYSQLI” feel free to ask us by commenting below or visit on our contact page. Thank you.

Other Articles you might like:

 

 

 

Facebook Comments
(Visited 150 times, 4 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here