gpt4 book ai didi

php - 图像无法显示,因为它包含错误警告

转载 作者:行者123 更新时间:2023-11-29 22:50:27 24 4
gpt4 key购买 nike

所以我想我永远不会得到答案哈哈......

老问题,请阅读编辑

我正在尝试将图片上传到 mysql 数据库后显示它。

但是通过尝试打开图片,Firefox 会显示以下警告:

the image cannot be displayed because it contains errors

我已经在谷歌上搜索了几个小时了,但我真的找不到解决这个问题的方法。所以现在你们是我最后的希望:)

这是 php:

    <?php
session_start();

$con = mysql_connect("localhost", "...", "...");
mysql_select_db('...',$con);

$id = $_GET['id'];
$result = mysql_query("SELECT image, mime FROM artikel WHERE id='$id'");

$row = mysql_fetch_object($result);
header("Content-type: $row->mime");
echo $row->image;
?>

编辑我认为错误是在 insert.php 中。有人可以看一下并告诉我可能出了什么问题吗?谢谢:)(我认为图片上传不正确)

<?php
session_start();
header('content-type: text/html; charset=utf-8');
$uname = $_POST['username'];
$typ = $_POST['typ'];
$titel = $_POST['titel'];
$content = $_POST['content'];
$timestamp = time();

$db = new mysqli("localhost", "...", "...", "...");
if($db->connect_errno > 0){
die("Unable to connect to database: " . $db->connect_error);
}

if(is_uploaded_file($_FILES['image']['tmp_name'])) {
// Verweis auf Bild
$image = $_FILES['image']['tmp_name'];

// Vorbereiten für den Upload in DB
$data = addslashes(file_get_contents($image));

// Metadaten auslesen
$meta = getimagesize($image);
$mime = $meta['mime'];
}

//create a prepared statement
$stmt = $db->set_charset("utf8");
$stmt = $db->prepare("INSERT INTO artikel (`red`, `typ`, `titel`, `content`, `image`, `mime`, `timestamp`) VALUES (?,?,?,?,?,?,?)");

//bind the username and message
$stmt->bind_param('sssssss', $uname, $typ, $titel, $content, $data, $mime, $timestamp);

//run the query to insert the row
$stmt->execute();

header("Location: erfolg.php");

?>

enter image description here

enter image description here

最佳答案

我按照here的指示进行操作。我插入了带有附加 mime 字段的图像并检查了代码是否正常工作。

确认您的 mime 类型是否正确。如果是,则图像可能已损坏。

简单地

header("Content-type: ");

也为我工作。 ScreenShot of my table Screenshot of the data

$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$row = mysql_fetch_object($result);



/*Only for testing purpose.*/
// echo $row->mime; //This line should output image/jpeg
/*Only for testing purpose*/


/*Only to check output*/
header("Content-type: $row->mime");

echo $row->image;
/*Only to check output*/

关于php - 图像无法显示,因为它包含错误警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930497/

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