Error updating data #image

0

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";

        }

    }

}
    
asked by ssbizkit 07.08.2018 в 02:26
source

2 answers

-1

Change the isset() method to file_exists ()

  

file_exists () Check if a file or directory exists

    
answered by 07.08.2018 / 18:52
source
1

I've done it that way

static public function save ProfileController () {

    $ruta = "";

    if(isset($_POST["nuevoUsuario"])){  

        if(isset($_FILES["nuevaImagen"]["tmp_name"])){

            $imagen = $_FILES["nuevaImagen"]["tmp_name"];

            $aleatorio = mt_rand(100, 999);


            $ruta = "./views/images/perfiles/perfil".$aleatorio.".jpg";





            $origen = imagecreatefromjpeg($imagen);

            $destino = imagecrop($origen, ["x"=>0, "y"=>0, "width"=>100, "height"=>100]);

            imagejpeg($destino, $ruta);

        }

        if($ruta == ""){

            $ruta = "views/images/photo.jpg";   

        }

        if(preg_match('/^[a-zA-Z0-9]+$/', $_POST["nuevoUsuario"])&&
           preg_match('/^[a-zA-Z0-9]+$/', $_POST["nuevoPassword"]) &&
           preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $_POST["nuevoEmail"])){

            $encriptar = crypt($_POST["nuevoPassword"], '$2a$07$asxx54ahjppf45sd87a5a4dDDGsystemdev$');

            $datosController = array("usuario"=>$_POST["nuevoUsuario"],
                                     "password"=>$encriptar,
                                     "email"=>$_POST["nuevoEmail"],
                                     "rol"=>$_POST["nuevoRol"],
                                     "photo"=> $ruta);

            $respuesta = GestorPerfilesModel::guardarPerfilModel($datosController, "usuarios");

            if($respuesta == "ok"){

                echo'<script>

                    swal({
                          title: "¡OK!",
                          text: "¡El usuario ha sido creado correctamente!",
                          type: "success",
                          confirmButtonText: "Cerrar",
                          closeOnConfirm: false
                    },

                    function(isConfirm){
                             if (isConfirm) {      
                                window.location = "perfil";
                              } 
                    });


                </script>';

            }

        }

        else{

            echo '<div class="alert alert-warning"><b>¡ERROR!</b> No ingrese caracteres especiales</div>';
        }

    }

}

#VISUALIZAR LOS PERFILES
#------------------------------------------------------------

  static  public function verPerfilesController(){

    $respuesta = GestorPerfilesModel::verPerfilesModel("usuarios"); 

    $rol = "";

    foreach($respuesta as $row => $item){

        if( $item["rol"] == 0){

            $rol = "Administrador";

          }

         else{

            $rol = "Editor";

          }
    
answered by 07.08.2018 в 17:05