X

Multiple File Upload with PHP And MySQL

In this tutorial we are going to learn how to will make a multi-file Upload with PHP, and to make a access to the upload document and we will save the document location in MySQL data. The system will automatically rename the file name so that if user upload any other document with same name it will also accepted by the system. The file includes images, PDF’s, Doc’s or any file types.

Creating Our DataBase

First, we have to create data base to store the data

  • Open phpMyAdmin
  • Create database
  • Then crate the table
  • And then insert the details Or
  • After creating a database name, click the SQL and paste the following code.
CREATE TABLE ` document` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `document_name` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
)

This table is to store the location of the every document uploaded the user.

Create config.php File

<?php
$con=mysqli_connect("localhost","root","","images") or die("Unable to connect Database");
?>

This is connection page from this page main file will be able to connect to the database.

Create  Location Folder

Firstly, we have to create a folder where all the document will be uploaded.

Main Screen

This page will appear whenever user open the site, in this page user have to browse for the document and click to the submit button.

Html Code

This is a simple HTML  code with form tag , inside form tag there is a browse and submit  button to get the details from user.

<html lang="en">
<head>
  <title>TechJunkGigs</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>Multiple File Upload Using PHP And MySQL</h2>
   <form method="POST" action-xhr="#" enctype="multipart/form-data">
    <div class="form-group row">
    <div class="form-group">
    <input type="file" name="files[]" class="col-xs-4 btn btn-default" id="file" multiple/>
	<input type="submit" name="submit" style="width:100px;" class="btn btn-primary" value="Upload">
    </div>
	</div>
  </form>
</div>
</body>

Make sure to make add enctype=”multipart/form-data  In form tag , type=”file”  and  name=”files[]” in input type of file to enable multi files selection.

Logical or PHP Code of multiple File Upload

This is PHPcode contains all logic to Upload the document.

<?php	
       include ("config.php");
       if(isset($_POST['submit'])){
       $valuefldr = 'document';
	foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
	$file_name = $key.$_FILES['files']['name'][$key];
	$file_size =$_FILES['files']['size'][$key];
	$file_tmp =$_FILES['files']['tmp_name'][$key];
	$file_type=$_FILES['files']['type'][$key];	
       $desired_dir= $valuefldr;
	if(move_uploaded_file($file_tmp,"$desired_dir/".$file_name))
	{
			$query="insert into document (document_name) VALUES('$file_name')";
			$result=mysqli_query($con,$query);  
	  ?>
	  <script>
	  alert('successfully');
			window.location.href='#';
			</script>
	  <?php
	 }
	 else
	 {
	  ?>
	  <script>
	  alert('error while uploading file');
			window.location.href='#';
			</script>
	  <?php
	 }
		 
	  }		
  }	  
?>    

As we are making an Document upload, we need to allowing all extensions i.e. JPEG, PNG,SQL,PDF … etc .

If are new to file uploading you can check our article on Simple file upload in php to Server, to get started with basic.

Demo Image

I hope this tutorial helped you to learn How to upload multiple file to database using PHP. To get the latest news and updates follow us on twitter facebook, subscribe to our YouTube channel.  And If you have any query then please let us know by using comment form.

 

Categories: CSS HTML MySQL PHP
Abhishek Kumar:
Related Post