Reputation: 41
Right i have a form that connects to a php script. With in the form there is multiple inputs such as text, checkbox, dropdown etc. However i also have 3 upload slots for 3 images that get uploaded to a img folder and i just grab the name to store in a mysql database along with the form.
here is the form.
<form method="post" action="scripts/AddProduct.php" enctype="multipart/form-data"><table class="table table-bordered table-striped">
<tr>
<td align="right">Category</td>
<td><label><select id="pro_catagory" name"pro_catagory">
<?php echo $result; ?>
</select>
</label></td>
</tr>
<tr>
<td align="right">Subcatagory</td>
<td><label>
<select name="pro_subcategory" id="pro_subcategory">
<option value="Hats">Hats</option>
</select>
</label></td>
</tr>
<tr>
<td align="right">Details</td>
<td><textarea name="pro_details" cols="" rows=""></textarea></td>
</tr>
<tr>
<td align="right">Size</td>
<td>
S
<input type="hidden" name="pro_size" value="0" />
<input type="checkbox" name="pro_size" value="1" />
M
<input type="hidden" name="pro_size_m" value="0" />
<input type="checkbox" name="pro_size_m" value="1" />
L
<input type="hidden" name="pro_size_l" value="0" />
<input type="checkbox" name="pro_size_l" value="1" />
</td>
</tr>
<tr>
<td align="right">Colour</td>
<td><label>
<input type="text" name="pro_colour" id="colour"/>
</label></td>
</tr>
<tr>
<td align="right">1st Image</td>
<td><label>
<input type="file" name="file" id="file" />
</label></td>
</tr>
<tr>
<td align="right">2nd Image</td>
<td><label>
<input type="file" name="filee" id="filee" />
</label></td>
</tr>
<tr>
<td align="right">3rd Image</td>
<td><label>
<input type="file" name="file3" id="file3" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="button" id="button" value="Add This Item Now" />
</label></td>
</tr> </table></form>
(I have shortend down the script so its only examples of what are in the form). Their are a total of 21 options in the form.
Foloowing this the php file AddProduct.php.
<?php
//File Upload 1
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br><br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}
//File Upload 2
if ($_FILES["filee"]["error"] > 0)
{
echo "Error: " . $_FILES["filee"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["filee"]["name"] . "<br>";
echo "Type: " . $_FILES["filee"]["type"] . "<br>";
echo "Size: " . ($_FILES["filee"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["filee"]["tmp_name"] . "<br><br>";
if (file_exists("upload/" . $_FILES["filee"]["name"]))
{
echo $_FILES["filee"]["name"] . " already exists. " . "<br><br>";
}
else
{
move_uploaded_file($_FILES["filee"]["tmp_name"],
"upload/" . $_FILES["filee"]["name"]);
}
}
//File Upload 3
if ($_FILES["file3"]["error"] > 0)
{
echo "Error: " . $_FILES["file3"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file3"]["name"] . "<br>";
echo "Type: " . $_FILES["file3"]["type"] . "<br>";
echo "Size: " . ($_FILES["file3"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file3"]["tmp_name"] . "<br><br>";
if (file_exists("upload/" . $_FILES["file3"]["name"]))
{
echo $_FILES["file3"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file3"]["tmp_name"],
"upload/" . $_FILES["file3"]["name"]);
}
}
//Rest of php to get the posted values (all still work)
?>
My problem is that when its submitted the name of file (image 1) is then followed through to filee and file3 so the output ends like:
Upload: testing.jpg
Type: image/jpeg
Size: 684.8818359375 kB
Stored in: C:\wamp\tmp\php41FF.tmp
testing.jpg already exists. Upload:
Type:
Size: 0 kB
Stored in:
already exists.
Upload:
Type:
Size: 0 kB
Stored in:
thanks for any help in advance
EDIT: this is var_dump
array (size=3)
'file' =>
array (size=5)
'name' => string '2Fourk. RV2.jpg' (length=15)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'C:\wamp\tmp\php4FD7.tmp' (length=23)
'error' => int 0
'size' => int 701319
'fileField2' =>
array (size=5)
'name' => string '2Fourk.jpg' (length=10)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'C:\wamp\tmp\php4FD8.tmp' (length=23)
'error' => int 0
'size' => int 938840
'fileField3' =>
array (size=5)
'name' => string 'fiverrgigs.png' (length=14)
'type' => string 'image/png' (length=9)
'tmp_name' => string 'C:\wamp\tmp\php4FE9.tmp' (length=23)
'error' => int 0
'size' => int 18322
Upvotes: 0
Views: 1177
Reputation: 2040
Just a suggestion. When you upload multiple files you should (well, I would) use one multiple upload file input.
<input type="file" name="file[]" id="file" multiple/>
And then in php work with this as with array.
if(isset($_FILES["file"]["name"])) {
for($i = 0; $i < count($_FILES['file']['name']); $i++) {
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"][$i] . "<br>";
}
else {
//do something with $_FILES["file"]["name"][$i];
}
}
}
The code will be shorter and you will do all things at "once".
Upvotes: 1
Reputation: 516
I think @drive235 makes a good suggestion
I made this short script. very dirty. but maybe it helps you, since you use many lines of scripts. Sometimes it is easier with less.
it iterates trough the $_FILES Array and shows the various image information.
foreach($_FILES as $v){
echo "<br />Upload: " . $v["name"]. "<br>";
echo "Type: " .$v["type"] . "<br>";
echo "Size: " . ($v["size"] / 1024) . " kB<br>";
echo "Stored in: " . $v["tmp_name"] . "<br><br>";
var_dump($v);
}
Upvotes: 1