problem with image path in db

0

I'm not a programmer, I'm doing some tutorials to create something for my particular use and I'm anchored in being able to show in my table an image that I upload to a folder on my server which also has a subfolder. my files:

  <tbody>
              <?php while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ?>
                 <tr>
                    <td><?php echo $row ['tecnico_id']; ?></td>
                    <td><?php echo $row ['nombre']; ?></td>
                    <td><?php echo $row ['telefono']; ?></td>
                    <td><?php echo $row ['direccion']; ?></td>
                    <td><?php echo $row ['email']; ?></td>
                    **<td><?php echo '<img src="'.$row['foto'].'" width="50px" height="50px">';?></td>**

                            <td>

guardartec.php

    <?php

    require 'conexion.php';

    $nombre = $_POST['nombre'];
    $telefono = $_POST['telefono'];
    $direccion = $_POST['direccion'];
    $email = $_POST['email'];
    $foto =$ruta.$_FILES['foto']['name'];


    $sql = "INSERT INTO tecnicos (nombre, telefono, direccion, email, foto) VALUES ('$nombre','$telefono','$direccion','$email','$foto')";
    $resultado = $mysqli->query($sql);

    $id_insert = $mysqli->insert_id;

    if($_FILES['foto']['error']>0){
        echo "Error al cargar archivo";
        } else {

        $permitidos = array('image/jpg','image/jpeg','image/png','application/pdf');
        $limite_kb = 200;

        if(in_array($_FILES['foto']['type'], $permitidos) && $_FILES['foto']['size'] <= $limite_kb * 1024){

            $ruta = 'fotos/'.$id_insert.'/';
            $foto = $ruta.$_FILES['foto']['name'];

            if(!file_exists($ruta)){
                mkdir($ruta);
            }

            if(!file_exists($foto)){

                $resultado = @move_uploaded_file($_FILES['foto']['tmp_name'], $foto);

                if($resultado){
               echo "Archivo Guardado";
                    } else {
                    echo "Error al guardar archivo";
                }

                } else {
                echo "Archivo ya existe";
            }

            } else {
            echo "Archivo no permitido o excede el tamaño";
        }

    }

?>




    <!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Control Service Robert</title>
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="publica/css/bootstrap.min.css">
    <link rel="stylesheet" href="publica/css/bootstrap-theme.css">
    <link rel="stylesheet" href="publica/estilos.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="publica/js/bootstrap.min.js"></script>
</head>

    <body>
        <div class="container">
            <div class="row">
                <div class="row" style="text-align:center">

                    <?php if($resultado) { ?>
                        <h3>REGISTRO GUARDADO</h3>
                        <?php } else { ?>
                        <h3>ERROR AL GUARDAR</h3>
                    <?php } ?>
                    <a href="tecnicos.php" class="btn btn-primary">Regresar</a>

                </div>
            </div>
        </div>
    </body>
</html>

updatetec.php

    <?php

    require 'conexion.php';

    $id = $_POST['id'];
    $nombre = $_POST['nombre'];
    $telefono = $_POST['telefono'];
    $direccion = $_POST['direccion'];
    $email = $_POST['email'];
    $foto =$ruta.$_FILES['foto']['name'];


    $sql = "UPDATE tecnicos SET nombre ='$nombre', telefono ='$telefono', direccion ='$direccion', email ='$email', foto ='$foto' WHERE tecnico_id ='$id'";
    $resultado = $mysqli->query($sql);

$id_insert =$id;

if($_FILES['foto']['error']>0){
        echo "Error al cargar archivo";
        } else {

        $permitidos = array('image/jpg','image/jpeg','image/png','application/pdf');
        $limite_kb = 200;

        if(in_array($_FILES['foto']['type'], $permitidos) && $_FILES['foto']['size'] <= $limite_kb * 1024){

            $ruta = 'fotos/'.$id_insert.'/';
            $foto = $ruta.$_FILES['foto']['name'];

            if(!file_exists($ruta)){
                mkdir($ruta);
            }

            if(!file_exists($foto)){

                $resultado = @move_uploaded_file($_FILES['foto']['tmp_name'], $foto);

                if($resultado){
                echo "Archivo Guardado";
                    } else {
                    echo "Error al guardar archivo";
                }

                } else {
                echo "Archivo ya existe";
            }

            } else {
            echo "Archivo no permitido o excede el tamaño";
         }

    }




?>


    <!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Control Service Robert</title>
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="publica/css/bootstrap.min.css">
    <link rel="stylesheet" href="publica/css/bootstrap-theme.css">
     <link rel="stylesheet" href="publica/estilos.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="publica/js/bootstrap.min.js"></script>
</head>

    <body>
        <div class="container">
            <div class="row">
                <div class="row" style="text-align:center">
                    <?php if($resultado) { ?>
                        <h3>REGISTRO MODIFICADO</h3>
                        <?php } else { ?>
                        <h3>ERROR AL MODIFICAR</h3>
                    <?php } ?>

                    <a href="tecnicos.php" class="btn btn-primary">Regresar</a>

                </div>
            </div>
        </div>
    </body>
</html>

In this line:

<td><?php echo '<img src="'.$row['foto'].'" width="50px" height="50px">';?></td>

how to do so that I write the path in the db in column photo well of the example mode carpeta/subcarpeta/imagen1.jpg I only achieve that I recorded imagen1.jpg in db.

For what I did, a subfolder with name was created according to the index for each photo uploaded I would be very grateful if you give me a hand.

    
asked by kuky 17.09.2018 в 06:03
source

1 answer

0

It seems that you are using the variable $ path before to initialize it

You use it here to do the update:

$email = $_POST['email'];
$foto =$ruta.$_FILES['foto']['name'];
$sql = "UPDATE tecnicos SET nombre ='$nombre', telefono ='$telefono', direccion ='$direccion', email ='$email', foto ='$foto' WHERE tecnico_id ='$id'";

Instead, the variable $ route you initialize later, after checking if the file is valid:

$ruta = 'fotos/'.$id_insert.'/';

I guess you'll have to do a new update to correctly save the file path after to move the file and check that it has moved well:

if ($resultado) {
    $sql = "UPDATE tecnicos SET foto = '$foto' WHERE tecnico_id ='$id'";
    $resultado = $mysqli->query($sql);
    // Comprobar resultado, etc...
    echo "Archivo Guardado";
}
    
answered by 17.09.2018 в 09:44