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:
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.