gpt4 book ai didi

php - 从 mysql 读取图像 blob 并显示它

转载 作者:行者123 更新时间:2023-11-29 20:58:35 24 4
gpt4 key购买 nike

我是 php、html 和 mysql 的新手,所以对我的问题表示歉意。

我正在将图像上传到 mysql(这部分工作正常),我现在正在尝试阅读它并将其显示在我的页面上。

这是我将其保存到 mysql 的代码

echo "<br>file selected run script";
echo "<br>file name : ".$_FILES['userfile']['name'];
echo "<br>file type : ".$_FILES['userfile']['type'];
echo "<br>file size : ".$_FILES['userfile']['size'];
$name = $_FILES['userfile']['name'];
$type = $_FILES['userfile']['type'];

$tmpName = $_FILES['userfile']['tmp_name'];
echo "$tmpName";
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$sql = "INSERT INTO images (image,name,type)
VALUES ('$data','$name','$type')";
$conn->exec($sql);

并且它有效。我有一个 HTML 文件。

loadimage.php 是:

$id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_NUMBER_INT); 尝试{

    /*** set the PDO error mode to exception ***/
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

/*** The sql statement ***/
$sql = "SELECT image,type FROM images WHERE id='$id'";
echo $sql;
/*** prepare the sql ***/
$stmt = $conn->prepare($sql);

/*** exceute the query ***/
$stmt->execute();

/*** set the fetch mode to associative array ***/
$stmt->setFetchMode(PDO::FETCH_ASSOC);

/*** set the header for the image ***/
$array = $stmt->fetch();

/*** check we have a single image and type ***/
if(sizeof($array) == 2)
{
/*** set the headers and display the image ***/
header("Content-type:".$array['type']);

/*** output the image ***/
echo $array['image'];
}
else
{
throw new Exception("Out of bounds Error");
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
}

其他 { echo '请使用真实身份证号码'; }

它确实连接到了 mysql,这不是问题。

但是 chrome 控制台给了我消息:

资源解释为文档,但使用 MIME 类型 image/jpeg 进行传输:“http://xxxxxxxxxxxxx/loadimage.php?id=12 ”。

当尝试获取我的图像 ID 12 时(所有图像给出相同的结果......)

最佳答案

我明白这一点,当我的代码真正起作用时我会解决这个问题

关于php - 从 mysql 读取图像 blob 并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com