Skip to content

Instantly share code, notes, and snippets.

@Mauryashubham
Created August 12, 2017 11:03
Show Gist options
  • Save Mauryashubham/0424c6fe82d2cabfb4608c3330aa757b to your computer and use it in GitHub Desktop.
Save Mauryashubham/0424c6fe82d2cabfb4608c3330aa757b to your computer and use it in GitHub Desktop.
Multiple Image Upload PHP form
/**
@author : Shubham Maurya,
Email id : [email protected]
**/
1.Make a file in notepad and save it as index.php and paste the below code.
<?php
$DB_host = "localhost";
$DB_name = "test";
$DB_user = "root";
$DB_pass = "";
try
{
$conn = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Done..";
}
catch(PDOException $e)
{
echo "ERROR : ".$e->getMessage();
}
if(isset($_POST['register']))
{
//$image1=$_POST['image1'];
//$ln=$_POST['image2'];
$imgFile = $_FILES['votercardimg']['name'];
$tmp_dir = $_FILES['votercardimg']['tmp_name'];
$imgSize = $_FILES['votercardimg']['size'];
$imgFile1 = $_FILES['adharimg']['name'];
$tmp_dir1 = $_FILES['adharimg']['tmp_name'];
$imgSize1 = $_FILES['adharimg']['size'];
$imgFile2 = $_FILES['ebillimg']['name'];
$tmp_dir2 = $_FILES['ebillimg']['tmp_name'];
$imgSize2 = $_FILES['ebillimg']['size'];
$imgFile3 = $_FILES['ecardimg']['name'];
$tmp_dir3 = $_FILES['ecardimg']['tmp_name'];
$imgSize3 = $_FILES['ecardimg']['size'];
$imgFile4 = $_FILES['otherimg']['name'];
$tmp_dir4 = $_FILES['otherimg']['tmp_name'];
$imgSize4 = $_FILES['otherimg']['size'];
$imgFile5 = $_FILES['drlincenceimg']['name'];
$tmp_dir5 = $_FILES['drlincenceimg']['tmp_name'];
$imgSize5 = $_FILES['drlincenceimg']['size'];
$upload_dir = 'images/'; // upload directory
$upload_dir1 = 'images/'; // upload directory
$upload_dir2 = 'images/'; // upload directory
$upload_dir3 = 'images/'; // upload directory
$upload_dir4 = 'images/'; // upload directory
$upload_dir5 = 'images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$imgExt1 = strtolower(pathinfo($imgFile1,PATHINFO_EXTENSION)); // get image extension
$imgExt2 = strtolower(pathinfo($imgFile2,PATHINFO_EXTENSION)); // get image extension
$imgExt3 = strtolower(pathinfo($imgFile3,PATHINFO_EXTENSION)); // get image extension
$imgExt4 = strtolower(pathinfo($imgFile4,PATHINFO_EXTENSION)); // get image extension
$imgExt5 = strtolower(pathinfo($imgFile5,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
$userpic1 = rand(1000,1000000).".".$imgExt1;
$userpic2 = rand(1000,1000000).".".$imgExt2;
$userpic3= rand(1000,1000000).".".$imgExt3;
$userpic4 = rand(1000,1000000).".".$imgExt4;
$userpic5 = rand(1000,1000000).".".$imgExt5;
// allow valid image file formatss
if(in_array($imgExt, $valid_extensions) & in_array($imgExt1, $valid_extensions) & in_array($imgExt2, $valid_extensions) & in_array($imgExt3, $valid_extensions) & in_array($imgExt4, $valid_extensions) & in_array($imgExt5, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000 & $imgSize1 < 5000000 & $imgSize2 < 5000000 & $imgSize3 < 5000000 & $imgSize4 < 5000000 & $imgSize5 < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
move_uploaded_file($tmp_dir1,$upload_dir.$userpic1);
move_uploaded_file($tmp_dir2,$upload_dir.$userpic2);
move_uploaded_file($tmp_dir3,$upload_dir.$userpic3);
move_uploaded_file($tmp_dir4,$upload_dir.$userpic4);
move_uploaded_file($tmp_dir5,$upload_dir.$userpic5);
try
{
$stmt=$conn->prepare("INSERT into image(image1,image2) VALUES(:image1,:image2)");
if($stmt->execute(array(':image1'=>$userpic,':image2'=>$userpic1)))
{
echo "insert";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
else{
echo '<script type="text/javascript">alert("Your File is too Large")</script>';
}
}
else{
echo '<script type="text/javascript">alert("Sorry, only JPG, JPEG, PNG & GIF files are allowed.")</script>';
}
}
?>
<html>
<body>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form method="post" role="form" enctype="multipart/form-data">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Upload the Image1</label>
<div style="line-height: 2.5;">
<p><input type="file" name="votercardimg" /></p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Upload the Image2</label>
<div style="line-height: 2.5;">
<p><input type="file" name="adharimg" /></p>
<p><input type="file" name="ebillimg" /></p>
<p><input type="file" name="ecardimg" /></p>
<p><input type="file" name="otherimg" /></p>
<p><input type="file" name="drlincenceimg" /></p>
</div>
</div>
</div>
<input type="submit" name="register">
</form>
</div><!-- /.8 -->
</div> <!-- /.row-->
</div> <!-- /.container-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment