I am close to 65% close to working in my gallery as I wanted. Can someone please tell me how I can upload multiple images to my gallery?
Here is the code.
Simple admin form code:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Category: <select class="text" name="dataType[]">
<option value="treeremoval" selected="selected">treeremoval</option>
<option value="treetrimming" >treetrimming</option>
<option value="treebracing" >treebracing</option>
<option value="stumpgrinding" >stumpgrinding</option>
<option value="firewood" >firewood</option>
<option value="cleanup" >cleanup</option>
</select>
<br /><br />
Caption: <input type="text" name="title[]">
<br /><br />
Image to upload: <input type="file" name="image[]" />
<br /><br />
Category: <select class="text" name="dataType[]">
<option value="treeremoval" selected="selected">treeremoval</option>
<option value="treetrimming" >treetrimming</option>
<option value="treebracing" >treebracing</option>
<option value="stumpgrinding" >stumpgrinding</option>
<option value="firewood" >firewood</option>
<option value="cleanup" >cleanup</option>
</select>
<br /><br />
Caption: <input type="text" name="title[]">
<br /><br />
Image to upload: <input type="file" name="image[]" />
<br /><br />
<input type="submit" value="Upload">
</form>
The uploader.php code:
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"][$i]);
$title = mysql_real_escape_string($_POST["title"][$i]);
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
$fileName = uniqid() . '.' . $fileData['extension'][$i];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$dataType = mysql_real_escape_string($_POST["dataType"][$i]); // get the dataType with the same key - $i
$title = mysql_real_escape_string($_POST["title"][$i]); // get the title with the same key - $i
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
while(file_exists($target_path))
{
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))
{ // The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
<a href='index.php'>Add another image</a><br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} // close your foreach
?>
I tried duplicating the form code but it didn't help Can someone please help me with this?