Help when updating the fields, the image that is already saved is deleted and the name is replaced by another one. I leave the code
public function actualizarEventosController(){
$ruta = "";
if(isset($_POST["descripcion"])){
if(isset($_FILES["editarImagen"]["tmp_name"])){
$imagen = $_FILES["editarImagen"]["tmp_name"];
$aleatorio = mt_rand(100, 999);
$ruta = "views/images/aricapro/banner".$aleatorio.".jpg";
$origen = imagecreatefromjpeg($imagen);
$destino = $origen; //imagecrop($origen, ["x"=>0, "y"=>0, "width"=>800, "height"=>400]);
imagejpeg($destino, $ruta);
$borrar = glob("views/images/aricapro/temp/*");
foreach($borrar as $file){
unlink($file);
}
}
if($ruta == ""){
$ruta = $_POST["editarPhoto"];
}
else{
unlink($_POST["editarPhoto"]);
}
$datosController = array( "id_even"=>$_POST["id_even"],
"dia"=>$_POST["diaEvento"],
"mes"=>$_POST["mesEvento"],
"categoria"=>$_POST["catEvento"],
"ano"=>$_POST["anoEvento"],
"ruta"=>$ruta,
"descripcion"=>$_POST["descripcion"]
);
$respuesta = EventosArica::actualizarEventosModel($datosController, "eventos");
if($respuesta == "success"){
echo '<script>
swal({
title: "¡OK!",
text: "¡El usuario ha sido editado correctamente!",
type: "success",
confirmButtonText: "Cerrar",
closeOnConfirm: false
},
function(isConfirm){
if (isConfirm) {
window.location = "eventos";
}
});
</script>';
}
else{
echo "error";
}
}
}