仅当一切为真或一个文件错误时上传文件才可以,如果其中一个文件为空,则使用 codeigniter 4 是错误的
upload file ok only if everything true or one file is wrong, if one of them empty this is wrong using codeigniter 4
我是 codeigniter 4 的新手,我有两个输入文件,它们在数据库中可以为 null,如果它们是 true 和我的控制器 select 图像,我的代码很好,如果其中一个不是图片,我的代码很好,但是如果其中一个是空的,我的代码是错误的,两个验证都被检查了,例如,如果配置文件是 selected 图像和 TestImage 不是 selected,结果说测试和死。请帮忙!谢谢。
在我看来:
<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>
我的控制器:
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;
谢谢
我找到了解决方案,
$ImageFile_1 = $this->request->getFile('ProfileUrl');
if ($ImageFile_1 && $ImageFile_1->getSize()>1)
如果我检查每个文件大小超过 1 个字节。
我是 codeigniter 4 的新手,我有两个输入文件,它们在数据库中可以为 null,如果它们是 true 和我的控制器 select 图像,我的代码很好,如果其中一个不是图片,我的代码很好,但是如果其中一个是空的,我的代码是错误的,两个验证都被检查了,例如,如果配置文件是 selected 图像和 TestImage 不是 selected,结果说测试和死。请帮忙!谢谢。
在我看来:
<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>
我的控制器:
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;
谢谢
我找到了解决方案,
$ImageFile_1 = $this->request->getFile('ProfileUrl');
if ($ImageFile_1 && $ImageFile_1->getSize()>1)
如果我检查每个文件大小超过 1 个字节。