Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

Multiple Image Upload in PHP

by
Category:php
php mysql
Multiple Image Upload in PHP

In this tutorial we will make a multi-file Upload with PHP system with verification for the file extension and size, to make a secure upload and save the file information into a MySQL database.

Create database and table

CREATE TABLE IF NOT EXISTS `file` (
  `file_id` int(10) NOT NULL AUTO_INCREMENT,
  `file_name` varchar(120) NOT NULL,
  `file_status` enum('0','1') NOT NULL COMMENT '0-active,1-inactive',
  PRIMARY KEY (`file_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=ut

config.php

<?php
$conn = @mysql_connect("localhost","root","");
mysql_select_db("phpcooker_script",$conn);
function valid_img($type){
    
    if($type=="image/gif" || $type=="image/jpeg" || $type=="image/jpg" || $type=="image/png"){
        return true;
    }else{<br>
        return false;
    }
}
?>

index.php

<?php
include"confing.php";
if(isset($_REQUEST['save'])){
    for($i=0;$i<count($_FILES['image']['name']);$i++){
        //image data handling
         $_FILES["image"]["size"][$i];
        if(valid_img($_FILES['image']['type'][$i])== true){<br>
            if($_FILES["image"]["size"][$i] <=  2097152){                
                 $image_name = rand()."_".$_FILES['image']['name'][$i];
                 move_uploaded_file($_FILES['image']['tmp_name'][$i],"upload/".$image_name);
                //insert query  and exicution of query
            $query = "insert into file(file_name,file_status) values('$image_name',1)";  
            if(mysql_query($query)){
                echo"<script>alert('data successfully saved ')</script>";
                //echo"<script>window.location='index.php'</script>";
            }else{
                echo"failed ".mysql_error();
            }
                
            }else{
                echo"<script>alert('image size not larger to 1 MB ')</script>";
            }
            
        }else{
            echo"<script>alert('invalid Image')</script>";
        }
        
    }    
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Multiple File Upload In PHP</title>
  <link rel="stylesheet" href="pagination.css">
  <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">
  <h1>Multiple File Upload In PHP</h1>
 
 <br />  <br />
 
 
 
         <form class="form-horizontal" method="post" enctype="multipart/form-data" >
    
    <div class="form-group has-success has-feedback">
          <label class="control-label">Select File</label>
    <input id="input-4" name="image[]" type="file" multiple class="file-loading">
    </div>
    

    <div class="form-group has-success has-feedback">
      
      <div class="col-sm-12" style="text-align:center;" >
       <button type="submit" class="btn btn-primary" name="save" value="save" >Upload</button>
      </div>
    </div>
     
  </form>

    
    
    
      <div class="row">
      <?php
        $query  = "select * from file order by file_id desc";
        $rs = mysql_query($query) or die("failed ".mysql_error());
        while($data = mysql_fetch_array($rs))
        {
            ?>
      
    <div class="col-md-4">
      <div class="thumbnail">
       
          <img src="upload/<?php echo $data["file_name"]; ?>" alt="Lights" style="width:100%">
          <div class="caption">

          </div>
        
      </div>
    </div>
          <?php     
        }
            
        ?>
  </div>
</div>
</body>
</html>

 


0 Comments


Html code work in comment box


Related Post