繁体   English   中英

从数据库php mysql检索时图像显示空白

[英]Images display blank when retrieving from database php mysql

我有四个文件:

  1. main.php我的html提交表单,用于提交图像和带有该图像的文本

  2. storeinfo.php,它将我所有的数据从html表单发送到工作的数据库,表单中的图像和文本已成功提交

  3. image.php从数据库中获取图像,并具有标头功能,可将aimagetype转换为png,jpeg ect等任何格式。

  4. show.php获取与图像一起发布的所有文本,并显示所有包含文本的图像但是图像不显示,而是当图像无法显示时出现空白框。

我找不到我的错误,我猜想它与image.php中的标头功能有关,或者当我尝试在show.php中使用html img标签显示图像时。 图像(存储为Blob)到数据库的上传成功。 为什么不显示图像?

每页按顺序编码:

  1. main.php的html形式

     <form enctype="multipart/form-data" action="storeinfo.php" method="POST"> <table border=0 align=center bgcolor=black width=100%> <tr><td colspan=2><h2>&nbsp</h2></td></tr> </table> <table border=0 align=center bgcolor=grey> <tr><td colspan=2><h2>Animal Information</h2></td></tr> <tr> <td>Name</td><td><input type=text name="aname"></td> </tr> <tr> <td>Description</td><td><input type=text name="adetails"></td> </tr> <tr> <td>Photo</td><td><input type=file name="aphoto"></td> </tr> <tr> <td></td><td><input type=submit name="submit" value="Store Information"></td> </tr> </table> </form> 
  2. storeinfo.php

     <?php $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $aname = $_POST['aname']; $adetails = $_POST['adetails']; $aphoto = addslashes (file_get_contents($_FILES['aphoto']['tmp_name'])); $image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc $imgtype = $image['mime']; $q ="INSERT INTO animaldata VALUES('','$aname','$adetails','$aphoto','$imgtype')"; $r = mysql_query($q,$conn); if($r) { echo "Information stored successfully"; } else { echo mysql_error(); } ?> 
  3. image.php

     <?php $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $id = $_GET['id']; $q = "SELECT aphoto,aphototype FROM animaldata where id='$id'"; $r = mysql_query("$q",$conn); if($r) { $row = mysql_fetch_array($r); $type = "Content-type: ".$row['aphototype']; header($type); echo $row['aphoto']; } else { echo mysql_error(); } ?> 
  4. show.php

     <?php //show information $conn = mysql_connect("localhost","root",""); if(!$conn) { echo mysql_error(); } $db = mysql_select_db("imagestore",$conn); if(!$db) { echo mysql_error(); } $q = "SELECT * FROM animaldata"; $r = mysql_query("$q",$conn); if($r) { while($row=mysql_fetch_array($r)) { //header("Content-type: text/html"); echo "</br>"; echo $row['aname']; echo "</br>"; echo $row['adetails']; echo "</br>"; //$type = "Content-type: ".$row['aphototype']; //header($type); //$lastid = mysql_insert_id(); // $lastid = $lastid; //echo "Your image:<br /><img src=image.php?id=$lastid />"; echo "<img src=image.php?id=".$row['id']." width=300 height=100/>"; } } else { echo mysql_error(); } ?> 

首先,我在以下位置找到了有关如何尝试执行的操作的教程: http : //www.mysqltutorial.org/php-mysql-blob/

第二,您应该使用mysql_escape_string(file_get_contents($ _ FILES ['aphoto'] ['tmp_name']))而不是添加斜线。

根据这两个规则,您应该能够找出代码的问题,也可以尝试使用较小的图片。

您的代码有很多问题,但是最值得注意的是您正在使用已过时的mysql函数,并且您的代码容易受到SQL注入攻击的攻击

我重写了storeinfo.phpimage.php以使用mysqli扩展并使用参数绑定来减轻SQL注入。 我将保留重写show.php作为练习。

请注意,我已经对表的结构进行了一些假设,因此您可能需要对SQL代码进行一些调整。

storeinfo.php

$aname = $_POST['aname'];
$adetails = $_POST['adetails'];
$aphoto = file_get_contents($_FILES['aphoto']['tmp_name']);
$image = getimagesize($_FILES['aphoto']['tmp_name']);//to know about image type etc
$imgtype = $image['mime'];

$conn = new mysqli("localhost","root","", "imagestore");
if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}

if (!($stmt = $conn->prepare("INSERT INTO animaldata (aname, adetails, aphoto, aphototype) VALUES(?, ?, ?, ?)"))) {
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
if (!$stmt->bind_param("ssbs", $aname, $adetails, $aphoto, $imgtype)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->send_long_data(2, $aphoto);

if (!$stmt->execute()) {
    echo "Insert failed: (" . $conn->errno . ") " . $conn->error;
} else {
    echo "Information stored successfully";
}

image.php

$conn = new mysqli("localhost","root","", "imagestore");
if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}

if (!($stmt = $conn->prepare("SELECT aphoto, aphototype FROM animaldata where id=?"))) {
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
if (!$stmt->bind_param("i", $_GET['id'])) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->execute()) {
    echo "Select failed: (" . $conn->errno . ") " . $conn->error;
} else {
    $stmt->bind_result($aphoto, $aphototype);
    $stmt->fetch();

    header("Content-type: ".$aphototype);
    echo $aphoto;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM