This question is an exact duplicate of:
I am creating a stock form which includes up to 20 image files. When uploading the images to the server i need to change the name and also insert them into a database so that it will work on the websites carousel. The name changes OK aswell as the upload to the servers folder. The problem I seem to be having is that if I CHOOSE ALL the images it works just fine, but if I CHOOSE less, (i.e; 1, 2, 3, 4 etc) then it breaks. I am probably over looking the obvious. Any help - Any ideas??
Here is where I am;
<?php
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
try {
if(!empty($_FILES['image']['name'][0])) {
$files = $_FILES['image'];
$i = 0;
$uploaded = array();
$failed = array();
$allowed = array('gif', 'png', 'jpg', 'jpeg');
foreach($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowed)) {
if($file_error === 0) {
if($file_size <= 2097152) {// 2MB
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'assets/uploads/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)) {
$uploaded[$position] = $file_destination;
$image = ($i === 0) ? 'image' : 'image_' . $i;
$i++;
} else {
$failed[$position] = "[{$file_name}] failed to upload";
}
} else {
$failed[$position] = "[{$file_name}] is too large";
}
} else {
$failed[$position] = "[{$file_name}] errored with code [{$file_error}]";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed";
}
}
}
$insert = DB::getInstance()->insert('stock', array(
'image' => $uploaded[0],
'image_1' => $uploaded[1],
'image_2' => $uploaded[2],
'image_3' => $uploaded[3],
'image_4' => $uploaded[4],
'image_5' => $uploaded[5],
'image_6' => $uploaded[6],
'image_7' => $uploaded[7],
'image_8' => $uploaded[8],
'image_9' => $uploaded[9],
'image_10' => $uploaded[10],
'image_11' => $uploaded[11],
'image_12' => $uploaded[12],
'image_13' => $uploaded[13],
'image_14' => $uploaded[14],
'image_15' => $uploaded[15],
'image_16' => $uploaded[16],
'image_17' => $uploaded[17],
'image_18' => $uploaded[18],
'image_19' => $uploaded[19]
));
} catch(Exception $e) {
die($e->getMessage());
}
}
}
?>
And the Form mockup;
<form action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label class="block"><strong>Upload Vehicle Images</strong><br>(Min = 1)<br>(Max = 20)</label><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="29"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="30"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="31"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="32"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="33"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="34"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="35"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="36"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="37"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="38"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="39"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="40"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="41"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="42"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="43"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="44"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="45"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="46"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="47"><br><br>
<input type="file" name="image[]" id="file" class="form-control" tabindex="48"><br>
</div>
</div>
</div>
<br>
<button class="btn btn-primary disabled" type="submit">FORM SUBMIT</button>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
Can anyone help?
</div>