Multiple Image Upload In PHP

multiple file upload in php

Multiple Image Upload In PHP :-

In This  Post We  Write Code For Multiple  File  Upload  In  Easy  Spates

creat database   :  phpcooker_script

create sqlfile :- db.sql  and  import

 


-- Database: `phpcooker_script`

--
-- Table structure for table `file`
--

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=utf8 AUTO_INCREMENT=15 ;

--
-- Dumping data for table `file`
--

INSERT INTO `file` (`file_id`, `file_name`, `file_status`) VALUES
(14, '9368_CMS_Creative_164657191_Kingfisher.jpg', '0'),
(13, '4115_beautiful_house-wallpaper-2560x1600.jpg', '0'),
(12, '28418_175789571.jpg', '0');

create  conf.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;
    }
}

?>

create  index.php

 



<?php

include"conf.php";

//for insert opretion

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>

Recommended Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Trusted by Ethereum Code Trading