I am new in codeigniter 4 and I have two input files, they can be null in database, with my controller if they are true and select image, my code is fine, if one of them not image, my code is fine, but if one of them is empty my code is wrong, both verification was checked, for example if profile be selected image and TestImage not selected, result say test and die. please help! thank you.
in my view:
<section class="col-12 col-lg-4">
<label for="ProfileUrl">Profile Image</label>
<input type="file" name="ProfileUrl" id="ProfileUrl" placeholder="choose your image"
class="form-control bg-input border-boot rounded-5"/>
<?php if ($validation->getError('ProfileUrl')): ?>
<div class='alert alert-danger mt-2'>
<?= $error = $validation->getError('ProfileUrl'); ?>
</div>
<?php endif; ?>
</section>
<section class="col-12 col-lg-4">
<label for="TestImage">test Image</label>
<input type="file" name="TestImage" id="TestImage" placeholder="choose your image"
class="form-control bg-input border-boot rounded-5"/>
</section>
my controller:
if ($ImageFile_2 = $this->request->getFile('ProfileUrl')):
$validated_2 = $this->validate([
'file' => [
'uploaded[ProfileUrl]',
'mime_in[ProfileUrl,image/jpg,image/jpeg,image/gif,image/png]',
'max_size[ProfileUrl,4096]',
]
]);
if ($validated_2) {
if ($ImageFile_2->isValid() && !$ImageFile_2->hasMoved()) {
$newName = $ImageFile_2->getRandomName();
$ImageFile_2->move(WRITEPATH . 'uploads', $newName);
$UploadPath = '' . WRITEPATH . 'uploads\\' . $newName;
$data_register['profile_img_url'] = $UploadPath;
}
} else {
echo 'profile';
die();
}
endif;
if (($ImageFile_1 = $this->request->getFile('TestImage'))):
$validated_1 = $this->validate([
'file' => [
'uploaded[TestImage]',
'mime_in[TestImage,image/jpg,image/jpeg,image/gif,image/png]',
'max_size[TestImage,4096]',
]
]);
if ($validated_1) {
if ($ImageFile_1->isValid() && !$ImageFile_1->hasMoved()) {
$newName = $ImageFile_1->getRandomName();
$ImageFile_1->move(WRITEPATH . 'uploads', $newName);
$UploadPath = '' . WRITEPATH . 'uploads\\' . $newName;
$data_register['test_img'] = $UploadPath;
}
} else {
echo 'test';
die();
}
endif;
thanks
I found a solution with this,
$ImageFile_1 = $this->request->getFile('ProfileUrl');
if ($ImageFile_1 && $ImageFile_1->getSize()>1)
if I check each file size more than 1 byte.