Upload images in PHP

2

I am working on a database to show images. I am preliminarily occupying XAMPP. I took a code from the web but I can not make it work.

I think I know where the problem is but I can not solve it. The code throws an error:

  

Fatal error: Call to a member function query () on resource ...

I think the error is in:

$result=$mysqli->query("SELECT * FROM album WHERE id=".$_GET["id"]);

In this part there is a quote open in SELECT and I think that's where the problem lies.

Main file

<body>
    <?php
    $mysqli=new mysqli("localhost","root","","imagenes");
    if (mysqli_connect_errno()) {
        die("Error al conectar: ".mysqli_connect_error());
    }
    if (isset($_FILES["userfile"]["tmp_name"]) and (is_uploaded_file($_FILES["userfile"]["tmp_name"]))) {
        if ($_FILES["userfile"]["type"]=="image/jpeg" || $_FILES["userfile"]["type"]=="image/pjpeg" || $_FILES["userfile"]["type"]=="image/gif" ||      $_FILES["userfile"]["type"]=="image/bmp" || $_FILES["userfile"]["type"]=="image/png") {
            $info=getimagesize($_FILES["userfile"]["tmp_name"]);
            $imagenEscapes=$mysqli->real_escape_string(file_get_contents($_FILES["userfile"]["tmp_name"]));
            $sql="INSERT INTO 'album' (anchura,altura,tipo,imagen) VALUES (".$info[0].",".$info[1].",'".$_FILES["userfile"]["type"]."','".$imagenEscapes."')";
            $mysqli->query($sql);
            $id=$mysqli->insert_id;
            echo "<div class='mensaje'>Imagen agregada con el id ".$id."</div>";
        }else{
            echo "<div class='error'>Error: El formato de archivo tiene que ser JPG, GIF, BMP o PNG.</div>";
        }
    }
    ?>
    <h2>Selecciona una imagen</h2>
    <form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">
        <input name="userfile" type="file">
        <p><input type="submit" value="Guardar Imagen">
    </form>
    <h2>Listado de las imagenes añadidas a la base de datos</h2>
    <div class="listadoImagenes">
        <?php
$result=$mysqli->query("SELECT * FROM album ORDER BY id DESC");
          if($result) {
              while($row=$result->fetch_array(MYSQLI_ASSOC)) {
                  echo "<img src='imagen_mostrar.php?id=".$row["id"]."' width='".$row["anchura"]."' height='".$row["altura"]."'>";        
              }
          }
        ?>
    </div>
</body>

File to show images

<?php
$mysqli=new mysqli("localhost","root","","imagenes");
if (mysqli_connect_errno()) {
die("Error al conectar: ".mysqli_connect_error());
}
$result=$mysqli->query("SELECT * FROM album WHERE id=".$_GET['id']);
$row=$result->fetch_array(MYSQLI_ASSOC);
header("Content-type:".$row["tipo"]);
echo $row['imagen'];
?>
    
asked by virtual8870 27.09.2016 в 01:06
source

1 answer

3

You are mixing APIs. You think you're using mysqli but you're actually using the obsolete mysql .

The way to get a connection mysqli is like this:

$connection = new mysqli($servername, $username, $password, $db);

Now you can make inquiries:

$connection->query('SELECT * FROM album ORDER BY id DESC'); // correcto

Update

To show the image in the view you have two options:

  • UUID
  • Base64 (if you save them in the database)

In your case the choice is Base64 :

echo base64_encode($row['imagen']);

And you show the image like this:

<img src="data:image/png;base64,AQUI_LA_BASE64" />
    
answered by 27.09.2016 в 01:28