无法从数据库中显示我的图像php

无法从数据库中显示我的图像php,php,image,Php,Image,问题是当它返回二进制文件时,请帮助我 <!DOCTYPE html> <html> <head> <title>Tienda Online</title> <FORM action="upload_imagen.php" enctype="multipart/form-data" method="POST"> <input type="file" name="imagen"> Busca

问题是当它返回二进制文件时,请帮助我

<!DOCTYPE html>
<html>
<head>
    <title>Tienda Online</title>

    <FORM action="upload_imagen.php" enctype="multipart/form-data" method="POST">
    <input type="file" name="imagen"> Buscar imagen
    <input type="submit" value="Buscar">
    </FORM>

    <div id="visualizar">

    </div>

    <form action="mostrar_imagen.php" method="POST">
    <input type="text" name="valor" >
    <input type="submit" value="mostrar">
    </form>

</html>

蒂恩达在线
客车图像
upload_imagen.php此文件上载一个图像并将其存储在数据库中

<?php

// Conexion a la base de datos
require "db_model.php";
class upload extends db_model {

   function whatever() {
       // Comprobamos si ha ocurrido un error.
       if (!isset($_FILES["imagen"]) || $_FILES["imagen"]["error"] > 0) {
            echo "Ha ocurrido un error.";
       } else {
            var_dump($_FILES["imagen"]);

            // Verificamos si el tipo de archivo es un tipo de imagen permitido.
            // y que el tamaño del archivo no exceda los 16MB
            $permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
            $limite_kb = 16384;

            if (in_array($_FILES['imagen']['type'], $permitidos) && $_FILES['imagen']['size'] <= $limite_kb * 1024) {
                // Archivo temporal
                $imagen_temporal = $_FILES['imagen']['tmp_name'];

                // Tipo de archivo
                $tipo = $_FILES['imagen']['type'];

                // Leemos el contenido del archivo temporal en binario.
                $fp = fopen($imagen_temporal, 'r+b');
                $data = fread($fp, filesize($imagen_temporal));
                fclose($fp);

                //Podríamos utilizar también la siguiente instrucción en lugar de las 3 anteriores.
                // $data=file_get_contents($imagen_temporal);
                // Escapamos los caracteres para que se puedan almacenar en la base de datos correctamente.
                $data = mysql_real_escape_string($data);

                // Insertamos en la base de datos.
                $this->query ="INSERT INTO imagenes (imagen, tipo_imagen) VALUES ('$data', '$tipo')";
                $resultado = $this->execute_query();
                if ($resultado) {
                    echo "El archivo ha sido copiado exitosamente.";
                } else {
                    echo "Ocurrió algun error al copiar el archivo.";
                }
            } else {
                echo "Formato de archivo no permitido o excede el tamaño límite de $limite_kb Kbytes.";
            }
        }
    }
}
$obj = new upload();
$obj->whatever();
?>

这可能是您的问题:

$data = mysql_real_escape_string($data);
在类中,您使用的是
mysqli.*
函数,但此函数属于不推荐使用的
mysql.*
函数。当您调用它时,它将自动尝试使用
mysql\u connect()
打开一个新的数据库连接,该连接将失败,因为它没有关于主机的任何信息
mysql\u real\u escape\u string()
然后将返回
false

Warning: mysql_real_escape_string(): A link to the server could not be established in test.php on line 2
bool(false)

只要删除该行,转义二进制数据很可能会破坏它。相反,您应该使用准备好的语句来防止SQL注入。

您收到了哪些错误消息?哪里出了问题?数据库中存储了什么?更多信息!SRY由于缺少信息,我没有任何错误消息,我的错误信息位于此处:header(“内容类型:image/jpg”);echo$imagen;在数据库中,图像被正确地存储,所以我可以下载它们,它们很好。首先,谢谢你的提醒,我改变了你说的那句话,是的,我错了mysql::mysqli,但错误并没有到此为止。。这是一个学习的应用程序,我知道我需要程序来防止sql入侵,但thnx可以说。我的问题是,当我试图显示图像时,会出现一个损坏的图像图标:(我可以从DB下载图像,它可以工作
$data = mysql_real_escape_string($data);
Warning: mysql_real_escape_string(): A link to the server could not be established in test.php on line 2
bool(false)