Hi,
I am the new one in PHP. I am getting some problem to upload multiple images to the database. I have two php files. First is images.php where I am generating the multiple <input> tags through javascript and in the second file images_save.php I want to access these multiple tages through for each loop but I dont know how to do this. Cany any one help me?
My actuall code is as follow:
images.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images</title>
<script type="text/javascript">
function addElement()
{
var actnum=document.getElementById("theValue");
var num=(document.getElementById("theValue").value-1)+2;
actnum.value=num;
if (num<=10)
{
var d=document.getElementById("contact_form");
var newDiv=document.createElement("div");
var newValue="div"+num;
newDiv.setAttribute("Id",newValue);
var newFile='<input type="file" name="images[]" />';
var newButton='<input type="button" name="'+ newValue +'" id="bImg'+num+'" value="Remove Image'+num+'" onclick=\'removeElement(this.name)\'>';
newDiv.innerHTML="Select Image : " + newFile + newButton;
d.appendChild(newDiv);
}
}
function removeElement(str)
{
var m=document.getElementById("contact_form");
var k=document.getElementById(str);
m.removeChild(k);
}
window.onload=addElement;
</script>
</head>
<body>
<div id="contact_form">
<form action="images_save.php" method="post" enctype="multipart/form-data">
<input type="hidden" value="0" name="theValue" id="theValue" />
<a href="#" onclick="addElement()">Add more Images</a>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</div>
</body>
</html>
images_save.php
<?php
if (isset($_POST['upload']))
{
foreach ($_FILES as $files)
{
$fileName=$_FILES['images']['name'];
$tmpName=$_FILES['images']['tmp_name'];
$fileType=$_FILES['images']['type'];
$fileSize=$_FILES['images']['size'];
$ext=substr(strchr($fileName,"."),1);
if (!file_exists("./UploadImages"))
{ mkdir("./UploadImages") ;}
$target_path="./UploadImages/";
$imageName=md5(rand()*time());
$target_path=$target_path . $imageName . ".". $ext;
move_uploaded_file($tmpName,$target_path);
$con=mysql_connect("localhost","root","elephant");
mysql_select_db("my_db",$con);
$query="insert into upload2 (fileName,fileType,fileSize,filePath) values('$imageName','$fileType','$fileSize','$target_path')";
if (!mysql_query($query))
{echo mysql_error();}
mysql_close($con);
}
?>