I am trying to save several images and several files in my projects table, composed of:
id, company_id, project_name, img, files, invoices, date
It works as follows:
if I only upload invoices-> nothing appears (should upload the files)
if i only upload photos-> they are created correctly (this is correct)
if I don't upload anything -> it tells me that there are no files (this is correct)
If I comment the condition of uploading images and the data referring to images and leave only invoices -> the files are uploaded properly
Attached image of the save function:
public function guardar(){
helper(['text','form', 'url']);
$proyectos = new ProyectosModel();
$validacion = $this->validate([
'nombre_proyecto' => 'required|min_length[3]',
'id_empresa' => 'required'
]);
$validacionIMG = $this->validate([
'img' => [
'uploaded[img]',
'mime_in[img,image/jpg,image/png,image/jpeg]',
'max_size[img,1024]'
]
]);
$validacionFacturas = $this->validate([
'facturas' => [
'uploaded[facturas]',
'mime_in[facturas,application/pdf]',
'max_size[facturas,4096]'
]
]);
$nombreRandom = random_string('numeric', 6);
$nombreRandomFactura = random_string('numeric', 6);
if (!$validacion){
$session = session();
$session->setFlashdata('mensaje','Por favor, revise la información');
return redirect()->back()->withInput();
}elseif($validacion && !$validacionIMG && !$validacionFacturas){
$data = [
'nombre_proyecto'=> $this->request->getVar('nombre_proyecto'),
'id_empresa'=> $this->request->getVar('id_empresa')
];
$proyectos->insert($data);
}elseif($validacion && $validacionIMG != null && !$validacionFacturas){
$nombreRandom = random_string('numeric', 6);
if ($this->request->getFileMultiple('img')) {
$imagenes = [];
foreach($this->request->getFileMultiple('img') as $file)
{
$nuevoNombre = $nombreRandom . '-' . $file->getName();
$file->move('./public/assets/img',$nuevoNombre);
$imagenes[] = $nuevoNombre;
}
$data = [
'nombre_proyecto'=> $this->request->getVar('nombre_proyecto'),
'id_empresa'=> $this->request->getVar('id_empresa'),
'img' => implode(",",$imagenes)
];
}
$proyectos->insert($data);
} elseif($validacion && $validacionFacturas != null && !$validacionIMG){
$nombreRandomFactura = random_string('numeric', 6);
if ($this->request->getFileMultiple('facturas')) {
$facturasGuardar = [];
foreach($this->request->getFileMultiple('facturas') as $file)
{
$nuevoNombres = $nombreRandomFactura . '-' . $file->getName();
$file->move('./public/assets/facturas',$nuevoNombres);
$facturasGuardar[] = $nuevoNombres;
}
$data = [
'nombre_proyecto'=> $this->request->getVar('nombre_proyecto'),
'id_empresa'=> $this->request->getVar('id_empresa'),
'facturas' => implode(",",$facturasGuardar)
];
}
$proyectos->insert($data);
}
return $this->response->redirect( site_url('/projectList'));
}
Attached form view:
<div class="form-group">
<label for="inputClientCompany">Imagen<small>(Elegir multiples imagenes con CTRL+click)</small>:</label><br>
<input type="file" name='img[]' multiple="" class="form-control">
<br>
<small>Archivos permitidos: jpg,jpeg,png. || Tamaño máximo -> 1mb</small>
</div>
<div class="form-group">
<label for="inputFileCompany">Facturas<small>(Elegir multiples PDFS con CTRL+click)</small>:</label><br>
<input type="file" name='facturas[]' multiple="" class="form-control">
<br>
<small>Archivos permitidos: pdf. || Tamaño máximo -> 1mb</small>
</div>
How can I upload both contents depends on my needs? Thank you!!!
Try this:
As you can see, I have practically rewritten your code changing these basic things:
elseif
my personal preference is to use switch() with a boolean operator, in this casetrue
and in eachcase
put the condition to match. As I say it is a personal preference, but it helps me to read my code better.$proyectos->insert($data);
outside the switch and each case , which now just calculates the value of $data, and put it in a conditional so that it either does the insert if $data has been filled in or shows a message warning as something has gone wrong:$this->validate
just before each of its following uses:because codeIgniter requires its use when multiple arrays are passed to the validate() method, as the code header of the reset() method in CI4 says, in system/Validation/Validation.php: