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
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 aname="images[]"
and after submit you can just see what you got byvar_dump( $_FILES )
.Basic snippet for uploading multiple images:
@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
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.You'd need to have multiple input fieldS:
and access each individual file with:
or you can use the PHP-specific array notation shortcut:
and
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.