I need to amend an image upload form so I can upload more than one image (6 to be exact). I've tried adding another input and changing the name value as suggested by others, but it still shows up with the same image duplicated from the first input. I know it's probably something simple, but I'm not a PHP developer so I haven't been able to find a solution.
Here's the code for uploading images:
if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] != '') {
$tempext = explode('.', $_FILES['image']['name']);
$tempext = strtolower($tempext[count($tempext) - 1]);
switch($tempext) {
case 'jpg':
case 'jpeg':
$ext = '.jpg';
break;
default:
$ext = false;
}
if($ext != false && move_uploaded_file($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext)) {
$imagesuccess = chmod($_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . $ext, 0644) ? 'yes' : 'no';
} else {
$imagesuccess = 'no';
}
}
And for the input:
<div class="control-container<?php if((isset($_FILES['image']) && !empty($_FILES['image']['name']))) echo ' error'; ?>">
<label for="image">Large Image</label>
<div class="multifield">
<?php
if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/img/profiles/' . $id . '.jpg')) {
echo '<img src="/img/profiles/' . $id . '.jpg?' . rand() . '" width="310px" />';
}
?>
<input type="file" name="image" id="image" />
</div>
</div>
Any help would be much appreciated.
Thanks
You'd need to have multiple input fieldS:
<input type="file" name="image1" />
<input type="file" name="image2" />
etc...
and access each individual file with:
$_FILES['image1'][...]
$_FILES['image2'][...]
etc...
or you can use the PHP-specific array notation shortcut:
<input type="file" name="image[]" />
<input type="file" name="image[]" />
and
$_FILES['image']['tmp_name'][0];
$_FILES['image']['tmp_name'][1];
etc...
Note that when using the array notation, PHP makes each individual data-point in the files array its own array. Instead of lumping a file's data together in a single arra, each individual bit is spread about across multiple arrays.
If you need to upload more that file, just change name
element. For example you can add additional input fields with JavaScript. Every element will have a name="images[]"
and after submit you can just see what you got by var_dump( $_FILES )
.
if you have 6 file inputs in your form, $_FILES should come with an array inside it. Check it with print_r($_FILES)
inside your processing file.
Basic snippet for uploading multiple images:
<html>
<?php
define("UPLOAD_DIR", "/path/to/uploads/");
for ($i = 0; $i < count($_FILES["file"]["tmp_name"]); $i++) {
if (is_uploaded_file($_FILES["file"]["tmp_name"][$i])) {
move_uploaded_file($_FILES["file"]["tmp_name"][$i], UPLOAD_DIR . $_FILES["file"]["name"][$i]);
}
}
?>
<body>
<form method="post" enctype="multipart/form-data">
<input name="file[]" type="file" /><br />
<input name="file[]" type="file" /><br />
<input name="file[]" type="file" /><br />
<input type="submit" />
</form>
</body>
</html>
@Ryan I think your problem might be within the html part why dont u use a single input it swill do what you whant for example
<form method="post" enctype="multipart/form-data">
<input name="file[]" type="file" multiple="true"><br />
<input type="submit" />
</form>