Error uploading images to the server

0

I hope you can help me, you will see I have the following: A file called index.php with the following code:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
<body style='background-image:url(fondo/wallpaper.jpg);background-attachment:fixed;background-repeat:no-repeat;background-position:50% 50%;'>

    <p>
        <form action="valida_foto.php" method="POST" enctype="multipart/form-data">
            <input type="file" name="foto" id="foto"><br>
            <input type="submit" name="enviar" value="Enviar">

        </form>    

        <?php
        require_once("clase/conexion.php");
        $query = $mysqli->query("SELECT * FROM imagen");
        while($res = $query->fetch_assoc()){
            echo '<img src="'.$res["foto"].'" width="100" heigth="100"><br>';
        }
        ?>
    </body>
</html>

And another file called valida_foto.php :

<?php
require_once("clase/conexion.php");
if(isset($_FILES['foto'])){
    $foto=$_FILES["foto"]["name"];
    $ruta=$_FILES["foto"]["tmp_name"];
    $destino="fotos/".$foto;
    copy($ruta,$destino);
            $query = $mysqli->query("INSERT INTO imagen(foto) VALUES('$destino')");

            header("Location: index.php");
            }

?>

And a folder called photos, where the photos that are uploaded will be stored, in this case I just try to save the route in the database, my base is called photos and the table is called < strong> image with the following fields image_id and photo . The problem that at the time I try to show the photo saved on the server, only shows the following:

    
asked by Anthony 07.01.2017 в 05:25
source

2 answers

0

After uploading your image, you have tried accessing that image from your browser, that is, assuming that your index.php is on localhost

http://localhost/fotos/imagen.jpg

or try to access

http://localhost/fotos

and you can view all the files that you have uploaded, click them and if you do not see the files it is likely to be a permission error which you can solve with

// Lectura y escritura para el propietario, lectura para los demás
chmod("/directorio/fichero", 0644);
    
answered by 07.01.2017 / 08:19
source
0

You need to verify that the file has been uploaded, consider this code for valida_foto.php :

<?php
require_once("clase/conexion.php");
if(isset($_FILES['foto'])){
  $destino="fotos/".$_FILES["foto"]["name"];
  if(move_uploaded_file($_FILES['foto']['tmp_name'], $destino)){
        $query = $mysqli->query("INSERT INTO imagen(foto) VALUES('$destino')");

        header("Location: index.php");

   }else{
     echo '<h1>Hay un error al subir el archivo.</h1>'
   }
} else {
  echo '<h1>No hay archivos para subir..</h1>'
}
    
answered by 08.01.2017 в 08:48