i have an image upload form, while adding the upload is working fine. Now my edit form looks like below:
<input type="hidden" name="pimage2old" value="<?=$val['pimage2']?>">
<?php
$selectedDays = $val['days'];
$titles = explode('|', $val['title']);
$pimages = explode('|', $val['pimage2']);
$details = explode('|', $val['details']);
$notes = explode('|', $val['note']);
?>
<div class="col-md-12 position-relative" id="dayRows" >
<?php for ($i = 1; $i <= 100; $i++) : ?>
<div class="row" style="margin-bottom:1%;<?= ($i <= $selectedDays) ? '' : 'display:none;' ?>" id="day<?php echo $i; ?>Row">
<div class="col-md-2 position-relative">
<label class="form-label" style="font-weight:bold">Day <?php echo $i; ?></label>
</div>
<div class="col-md-2 position-relative">
<label class="form-label" for="validationTooltip14">Title</label>
<input class="form-control" id="validationTooltip14" type="text" name="title[]" value="<?=$titles[$i-1]?>" onkeypress="return RestrictCommaSemicolon(event);"
ondrop="return false;" onfocusout="checkComma(this);">
</div>
<div class="col-md-2 position-relative">
<label class="form-label" for="validationTooltip15">Image: <?php if($pimages[$i-1]){?><a target="_blank" href="<?php echo base_url()?>uploads/packages/<?=$pimages[$i-1]?>">View Image</a><?php } ?></label>
<input class="form-control" id="validationTooltip15" type="file" name="pimage2[]" >
</div>
<div class="col-md-3 position-relative">
<label class="form-label" for="validationTooltip16">Details</label>
<textarea class="form-control" id="validationTooltip16" name="details[]" onkeypress="return RestrictCommaSemicolon(event);"
ondrop="return false;" onfocusout="checkComma(this);"><?=$details[$i-1]?></textarea>
</div>
<div class="col-md-3 position-relative">
<label class="form-label" for="validationTooltip17">Note</label>
<textarea class="form-control" id="validationTooltip17" name="note[]" onkeypress="return RestrictCommaSemicolon(event);"
ondrop="return false;" onfocusout="checkComma(this);"><?=$notes[$i-1]?></textarea>
</div>
</div>
<?php endfor; ?>
</div>
and my controller to handle images is like
if (!empty($_FILES['pimage2']['name'])) {
$config2['upload_path'] = './uploads/packages/';
$config2['allowed_types'] = '*';
$this->load->library('upload');
$images = array();
$filesCount = count($_FILES['pimage2']['name']);
$oldImages = explode('|', $details['pimage2old']);
for ($i = 0; $i < $filesCount; $i++) {
if (!empty($_FILES['pimage2']['name'][$i])) {
$_FILES['userFile']['name'] = $_FILES['pimage2']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['pimage2']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['pimage2']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['pimage2']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['pimage2']['size'][$i];
$this->upload->initialize($config2);
if ($this->upload->do_upload('userFile')) {
$uploadData2 = $this->upload->data();
$images[$i] = $uploadData2['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
var_dump($error);
$images[$i] = '';
}
} else {
$images[$i] = $oldImages[$i];
}
}
// Concatenate the image names with a pipe separator
$picture2 = implode('|', $images);
} else {
$picture2 = $details['pimage2old'];
}
here the upload is not working, if i replace any specific image or add an image to a field where image is not there, its not happening, previous images are not replaced also, what could be the issue here, thanks in advance
If you use if (!empty($_FILES['pimage2']['name'][0]))
it'll only process the uploaded images if a new image was uploaded for the first field.
Your intention is probably to check if any images were uploaded, and if so process them all.
But with your form setup, $_FILES['pimage2']['name']
will always have a value (if no images were uploaded this'll contain an array with empty strings) so if (!empty($_FILES['pimage2']['name']))
will always return true. So there's no easy way to check if any images were uploaded unless you loop through the $_FILES
array and check them one by one.
And since you're already doing that in the inner loop with if (!empty($_FILES['pimage2']['name'][$i]))
, I would suggest removing the outer if
altogether:
$config2['upload_path'] = './uploads/packages/';
$config2['allowed_types'] = '*';
$this->load->library('upload');
$images = array();
$filesCount = count($_FILES['pimage2']['name']);
$oldImages = explode('|', $details['pimage2old']);
for ($i = 0; $i < $filesCount; $i++) {
if (!empty($_FILES['pimage2']['name'][$i])) {
$_FILES['userFile']['name'] = $_FILES['pimage2']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['pimage2']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['pimage2']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['pimage2']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['pimage2']['size'][$i];
$this->upload->initialize($config2);
if ($this->upload->do_upload('userFile')) {
$uploadData2 = $this->upload->data();
$images[$i] = $uploadData2['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
var_dump($error);
$images[$i] = '';
}
} else {
$images[$i] = $oldImages[$i];
}
}
// Concatenate the image names with a pipe separator
$picture2 = implode('|', $images);