How about friends, I hope you are in the best I am making an API which allows you to upload data through it, if you insert the data that I require but I have run into a problem which I cannot get out of there
I have the following form.php which works fine for me but what I require is that I don't need the image input
<form action="add.php" method="POST" enctype="multipart/form-data">
<input type="text" name="Cliente" placeholder="Nombre Cliente">
<input type="text" name="rfc" placeholder="RFC">
<input type="text" name="moneda" placeholder="Tipo de Moneda">
<input type="text" name="mercancia" placeholder="Nueva o Usada">
<input type="text" name="importe" placeholder="Importe Asegurado">
<input type="text" name="TipoOperacion" placeholder="Bien Asegurado">
<input type="datetime-local" name="FechaAlta" value="<?php echo date('Y-m-d\TH:i');?>">
<input type="file" name="imagen" placeholder="">
<input type="submit" value="Enviar">
</form>
To which my problem enters this part, how could the second if be left so that instead of the image it is some other field or the image is optional and not mandatory, since just like this if I do not add an image it does not insert data
if(isset($_POST['Cliente'])){
if($api->subirImagen($_FILES['imagen'])){
$item = array(
'Cliente' => $_POST['Cliente'],
'rfc' => $_POST['rfc'],
'moneda' => $_POST['moneda'],
'mercancia' => $_POST['mercancia'],
'importe' => $_POST['importe'],
'TipoOperacion' => $_POST['TipoOperacion'],
'FechaAlta' => $_POST['FechaAlta'],
'imagen' => $api->getImagen()
);
$api->add($item);
}else{
$api->error('Error con el archivo: ' . $api->getError());
}
}else{
$api->error('Error al llamar a la API');
}
Here is the uploadImage function , what it does is that it saves the image in a folder called images which should not weigh more than 5 megabytes, it validates if it is jpg or png and if it is not, it does not allow you to insert information in the base of data until you meet the requirements of the image
But the truth is that I am no longer interested in uploading an image but if I do not put it forcibly it does not save anything, but I already tried to modify the second IF uploadImage
function subirImagen($file){
$directorio = "imagenes/";
$this->imagen = basename($file["name"]);
$archivo = $directorio . basename($file["name"]);
$tipoArchivo = strtolower(pathinfo($archivo, PATHINFO_EXTENSION));
// valida que es imagen
$checarSiImagen = getimagesize($file["tmp_name"]);
if($checarSiImagen != false){
//validando tamaño del archivo
$size = $file["size"];
if($size > 500000){
$this->error = "El archivo tiene que ser menor a 500kb";
return false;
}else{
//validar tipo de imagen
if($tipoArchivo == "jpg" || $tipoArchivo == "jpeg" || $tipoArchivo == "png"){
// se validó el archivo correctamente
if(move_uploaded_file($file["tmp_name"], $archivo)){
//echo "El archivo se subió correctamente";
return true;
}else{
$this->error = "Hubo un error en la subida del archivo";
return false;
}
}else{
$this->error = "Solo se admiten archivos jpg/jpeg/png";
return false;
}
}
}else{
$this->error = "El documento no es una imagen";
return false;
}
}
If you are not interested in uploading any images then:
1. In the form
Remove the
input
one that picks up the image. Note that I have also removed the formenctype="multipart/form-data"
which should be put in those forms through which you send attachments. Since you will not send the image I have removed it, leaving it like this:2. On the server
Removes all conditionals related to the image, as well as the function call. Also, what you build as
$item
is the same as what you already have in$_POST
, since$_POST
it is nothing more than an array exactly the same as the one you are creating. That means you can pass directly$_POST
as a parameter to the methodadd()
.The code would look like this: