multiple image upload to server using php mysql
index.php
<!DOCTYPE html>
<html>
<head>
<title> Dynamically create input fields- jQuery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var addDiv = $('#addinput');
var i = $('#addinput p').size() + 1;
var max_fields=10;
$('#addNew').live('click', function() {
if(i<max_fields){
$('<p><input type="file" id="myimage" size="40" name="myimage' + i +'" value="" placeholder="I am New" /><a href="#" id="remNew">Remove</a> </p>').appendTo(addDiv);
i++;
}
return false;
});
$('#remNew').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
</script>
</head>
<body>
<h2>Dynammically Add New Input Box</h2>
<form method="POST" action="store_image.php" enctype="multipart/form-data">
<div id="addinput">
<p>
<input type="file" id="myimage" size="20" name="myimage" value="" placeholder="Input Value" /><a href="#" id="addNew">Add</a>
</p>
</div>
<input type="submit" name="submit_image" value="Upload">
</form>
</body>
</html>
store_image.php
<?php
error_reporting(E_ERROR | E_PARSE);
?>
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname='demo';
$conn=mysqli_connect($host, $user, $pass, $dbname);
//mysql_select_db('demo');
$upload_image=$_FILES["myimage"][ "name" ];
$upload_image2=$_FILES["myimage2"][ "name" ];
$upload_image3=$_FILES["myimage3"][ "name" ];
$upload_image4=$_FILES["myimage4"][ "name" ];
$upload_image5=$_FILES["myimage5"][ "name" ];
$upload_image6=$_FILES["myimage6"][ "name" ];
$upload_image7=$_FILES["myimage7"][ "name" ];
$upload_image8=$_FILES["myimage8"][ "name" ];
$upload_image9=$_FILES["myimage9"][ "name" ];
$upload_image10=$_FILES["myimage10"][ "name" ];
$folder="images/";
move_uploaded_file($_FILES["myimage"]["tmp_name"], "$folder".$_FILES["myimage"]["name"]);
move_uploaded_file($_FILES["myimage2"]["tmp_name"], "$folder".$_FILES["myimage2"]["name"]);
move_uploaded_file($_FILES["myimage3"]["tmp_name"], "$folder".$_FILES["myimage3"]["name"]);
move_uploaded_file($_FILES["myimage4"]["tmp_name"], "$folder".$_FILES["myimage4"]["name"]);
move_uploaded_file($_FILES["myimage5"]["tmp_name"], "$folder".$_FILES["myimage5"]["name"]);
move_uploaded_file($_FILES["myimage6"]["tmp_name"], "$folder".$_FILES["myimage6"]["name"]);
move_uploaded_file($_FILES["myimage7"]["tmp_name"], "$folder".$_FILES["myimage7"]["name"]);
move_uploaded_file($_FILES["myimage8"]["tmp_name"], "$folder".$_FILES["myimage8"]["name"]);
move_uploaded_file($_FILES["myimage9"]["tmp_name"], "$folder".$_FILES["myimage9"]["name"]);
move_uploaded_file($_FILES["myimage10"]["tmp_name"], "$folder".$_FILES["myimage10"]["name"]);
$insert_path="INSERT INTO image_table(imagename, imagename2, imagename3, imagename4, imagename5, imagename6, imagename7, imagename8, imagename9, imagename10, imagecontent) VALUES('$upload_image', '$upload_image2', '$upload_image3', '$upload_image4', '$upload_image5', '$upload_image6', '$upload_image7', '$upload_image8', '$upload_image9', '$upload_image10', '$folder')";
$var=mysqli_query($conn, $insert_path);
echo "Images uploaded";
?>
<!DOCTYPE html>
<html>
<head>
<title> Dynamically create input fields- jQuery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var addDiv = $('#addinput');
var i = $('#addinput p').size() + 1;
var max_fields=10;
$('#addNew').live('click', function() {
if(i<max_fields){
$('<p><input type="file" id="myimage" size="40" name="myimage' + i +'" value="" placeholder="I am New" /><a href="#" id="remNew">Remove</a> </p>').appendTo(addDiv);
i++;
}
return false;
});
$('#remNew').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
</script>
</head>
<body>
<h2>Dynammically Add New Input Box</h2>
<form method="POST" action="store_image.php" enctype="multipart/form-data">
<div id="addinput">
<p>
<input type="file" id="myimage" size="20" name="myimage" value="" placeholder="Input Value" /><a href="#" id="addNew">Add</a>
</p>
</div>
<input type="submit" name="submit_image" value="Upload">
</form>
</body>
</html>
store_image.php
<?php
error_reporting(E_ERROR | E_PARSE);
?>
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname='demo';
$conn=mysqli_connect($host, $user, $pass, $dbname);
//mysql_select_db('demo');
$upload_image=$_FILES["myimage"][ "name" ];
$upload_image2=$_FILES["myimage2"][ "name" ];
$upload_image3=$_FILES["myimage3"][ "name" ];
$upload_image4=$_FILES["myimage4"][ "name" ];
$upload_image5=$_FILES["myimage5"][ "name" ];
$upload_image6=$_FILES["myimage6"][ "name" ];
$upload_image7=$_FILES["myimage7"][ "name" ];
$upload_image8=$_FILES["myimage8"][ "name" ];
$upload_image9=$_FILES["myimage9"][ "name" ];
$upload_image10=$_FILES["myimage10"][ "name" ];
$folder="images/";
move_uploaded_file($_FILES["myimage"]["tmp_name"], "$folder".$_FILES["myimage"]["name"]);
move_uploaded_file($_FILES["myimage2"]["tmp_name"], "$folder".$_FILES["myimage2"]["name"]);
move_uploaded_file($_FILES["myimage3"]["tmp_name"], "$folder".$_FILES["myimage3"]["name"]);
move_uploaded_file($_FILES["myimage4"]["tmp_name"], "$folder".$_FILES["myimage4"]["name"]);
move_uploaded_file($_FILES["myimage5"]["tmp_name"], "$folder".$_FILES["myimage5"]["name"]);
move_uploaded_file($_FILES["myimage6"]["tmp_name"], "$folder".$_FILES["myimage6"]["name"]);
move_uploaded_file($_FILES["myimage7"]["tmp_name"], "$folder".$_FILES["myimage7"]["name"]);
move_uploaded_file($_FILES["myimage8"]["tmp_name"], "$folder".$_FILES["myimage8"]["name"]);
move_uploaded_file($_FILES["myimage9"]["tmp_name"], "$folder".$_FILES["myimage9"]["name"]);
move_uploaded_file($_FILES["myimage10"]["tmp_name"], "$folder".$_FILES["myimage10"]["name"]);
$insert_path="INSERT INTO image_table(imagename, imagename2, imagename3, imagename4, imagename5, imagename6, imagename7, imagename8, imagename9, imagename10, imagecontent) VALUES('$upload_image', '$upload_image2', '$upload_image3', '$upload_image4', '$upload_image5', '$upload_image6', '$upload_image7', '$upload_image8', '$upload_image9', '$upload_image10', '$folder')";
$var=mysqli_query($conn, $insert_path);
echo "Images uploaded";
?>
Comments
Post a Comment