Upload Multiple Files in PHP /MYSQL

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.

[sql]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;[/sql]

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

[php]<?php
$conn = new mysqli(‘localhost’, ‘root’, ”, ‘itsourcecode’);

if ($conn->connect_error) {
echo $conn->error;
}
?>[/php]

On the index file. Put the following codes.

[php]<?php
include ‘conn.php’;
?>[/php]

[html]<html>
<head>
<meta charset=”UTF-8″>
<title>Upload Multiple Files</title>
<link href=”assets/css/bootstrap.css” rel=”stylesheet” type=”text/css”/>
<style>
body {
padding-top: 10px;
}
</style>
</head>
<body>

[/html]

[php]File/s Uploaded!

‘;
}
}
?>[/php]

[html]<h3>Upload files:</h3>
<form method=”post” action=”upload-action.php” enctype=”multipart/form-data” role=”form”>
<input type=”file” name=”file1″ /><br>
<input type=”file” name=”file2″ /><br>
<input type=”file” name=”file3″ /><br>

Reset

</form>
</div>[/html]

[php]<?php
$files = $conn->query(“SELECT * FROM files”);
?>[/php]

[html]
</div>
</body>
</html>
[/html]

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

[php]<?php
include ‘conn.php’;
if (isset($_POST[‘upload’])) {
if (isset($_FILES[‘file1’]) && $_FILES[‘file1’][‘name’] != null) {
$file1_name = addslashes($_FILES[‘file1’][‘name’]);

move_uploaded_file($_FILES[‘file1’][‘tmp_name’], ‘files/’. $_FILES[‘file1’][‘name’]);
$filelink1 = “files/”. $_FILES[‘file1’][‘name’];

$upload_file_1 = $conn->query(“INSERT INTO files (file_name, file_link) VALUES (‘$file1_name’, ‘$filelink1’)”);

if ($upload_file_1) {
header(“Location: index.php?upload_action=uploaded_successful”);
} else {
echo $conn->error;
}
}
if (isset($_FILES[‘file2’]) && $_FILES[‘file2’][‘name’] != null) {
$file2_name = addslashes($_FILES[‘file2’][‘name’]);

move_uploaded_file($_FILES[‘file2’][‘tmp_name’], ‘files/’. $_FILES[‘file2’][‘name’]);
$filelink2 = “files/”. $_FILES[‘file2’][‘name’];

$upload_file_2 = $conn->query(“INSERT INTO files (file_name, file_link) VALUES (‘$file2_name’, ‘$filelink2’)”);

if ($upload_file_2) {
header(“Location: index.php?upload_action=uploaded_successful”);
} else {
echo $conn->error;
}
}
if (isset($_FILES[‘file3’]) && $_FILES[‘file3’][‘name’] != null) {
$file3_name = addslashes($_FILES[‘file3’][‘name’]);

move_uploaded_file($_FILES[‘file3’][‘tmp_name’], ‘files/’. $_FILES[‘file3’][‘name’]);
$filelink3 = “files/”. $_FILES[‘file3’][‘name’];

$upload_file_3 = $conn->query(“INSERT INTO files (file_name, file_link) VALUES (‘$file3_name’, ‘$filelink3’)”);

if ($upload_file_3) {
header(“Location: index.php?upload_action=uploaded_successful”);
} else {
echo $conn->error;
}
}
}
?>[/php]

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:

2 thoughts on “Upload Multiple Files in PHP /MYSQL”

Leave a Comment