how to upload multiple files/Images in php and store in database - Web Development and Web Design Codes

Latest

Saturday, May 27, 2017

how to upload multiple files/Images in php and store in database

PHP MySQL Multiple Image/Files Upload and Display

php mysql upload multiple images
php mysql upload multiple images

In This Lesson we will Learn how to Upload and Save Multiple Files/Image Into Database using PHP and MySQL.
You can Upload any Image/Files Easily To Desired Folder and Save Information to Database.
##What You will Need:
##Create create 4 files and 1 folder named 'images' in same folder.
1.connect.php
2.index.php
3.upload.php
4.style.css
5. images [folder name]
php upload multiple images

Database:
 Database Name: test
Table name: image
Using The Below SQL Commands for Creating Table 'image'
CREATE TABLE IF NOT EXISTS `image` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `size` varchar(100) NOT NULL,
  `type` varchar(10) NOT NULL,
    PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 

Now copy The Below Codes for Certain File.
connect.php:
<?php
// Creating connection test is our database name
$conn = new mysqli('localhost', 'root', '','test');
// Checking connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>
 

index.php:
Create Index page to Display Uploaded images.
<head>
<title>PHP MySQL Multiple Images Upload</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2 align="center">Select Multiple Files to Upload</h2>
<div id="content">
<div class="txt-heading">Uploaded Images</div>
<?php 
include("connect.php");
include("upload.php");
$result=mysqli_query($conn,"SELECT*FROM image ORDER by ID DESC");
$count=$result->num_rows;
if($count==0){
 echo "No Image Found";
}else{
 while($row=$result->fetch_array()){?>
     <ul>
    <li><img src="images/<?php echo $row['name'];?>"/></li>
  </ul>
<?php }}?>   
  
</div>
</body>
</html>
 
upload.php:
Create Upload.php file to Display Upload form and Proceed Selected Images To Upload.
<html>
<head>
<title>Upload Multiples Images Using PHP MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<div id="form">
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="images[]" multiple required/>
<input type="submit" name="submit" value="Upload"/>
</form>
</div>
</html>
<?php
include("connect.php");
if(isset($_FILES['images'])){
 $errors= array();
 foreach($_FILES['images']['tmp_name'] as $key =>$tmp_name){
  $file_name=$_FILES['images']['name'][$key];
  $file_size=$_FILES['images']['size'][$key];
  $file_tmp=$_FILES['images']['tmp_name'][$key];
  $target="images";
   $upload=move_uploaded_file($file_tmp,"$target/".$file_name);
  if($upload==TRUE){
 $result=mysqli_query($conn,"INSERT INTO image(name,size)VALUES('$file_name','$file_size')");
}else{
 echo 'Upload Failed';
} 
}
 if($upload==TRUE){
  echo "Your Files Successfully Uploaded and Information Saved in Our Database";
 }
  } 
?>

style.css:
Create some CSS Codes to Design Our Index Page.
#content{
 width:700px;
 margin:auto;
}
.txt-heading{
 background-color: #FFD0A6;
    padding-top:4px;
 border-bottom:2px solid green;
 margin-bottom:5px;
}
ul{
 list-style:none;
}
ul li{
 float:left;
 margin-left:5px;
 margin-top:5px;
}
ul li img{
 width:200px;
 height:200px;
}
#form{
 width:700px;
 background:#42f48c;
 padding:5px 8px;
 border-radius:5px;
}
input[type=file]{
 padding:5px 0px 5px 5px;
 background:black;
 border-radius:5px;
}
input[type=submit]{
 background:black;
 color:white;
 padding:6px 8px;
 font-weight:bold;
 cursor:pointer;
 border-radius:5px;
}

Now You have Successfully Created PHP MySQL Multiple Files/Image Upload and Display System.
Please Upload Image First to Get result.
Thank You Visiting Our Blog.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.