2015-03-08 40 views
0

所以我想我永远也不会得到答案哈哈...无法显示图像,因为它包含错误警告

老问题,请阅读编辑

我想显示图片上传到一个MySQL数据库后。

但是通过尝试打开图片火狐显示以下警告:

无法显示图像,因为它包含错误

我已经被好几个小时,现在搜索,因为它的谷歌但我真的无法找到解决这个问题。所以,现在你们是我非常最后的希望:)

这里是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

+0

您是否尝试在注释中设置'header'?这样你会看到脚本抛出的任何警告/错误。在将脚本强制为图像之前,请确保没有警告和错误 – DarkBee 2015-03-08 19:23:18

+0

您确定从中获取图像的表称为'artikel'吗? – l0lander 2015-03-08 19:23:36

+0

@ l0lander是的,我是:)这是文章的德语含义。 – 2015-03-08 19:24:30

回答

0

我跟着从here指令。我用额外的MIME字段插入图像,并检查代码正在工作。

确认您的mime类型是否正确。如果是的话,那么图像可能会被破坏。

具有简单

header("Content-type: "); 

也为我工作。 ScreenShot of my tableScreenshot 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*/ 
+0

这是我的工作文件。您可以下载它并在本地服务器上测试。 http://iseedtechnologies.in/other/downloads/stackoverflowImageEnquiry.tar.gz – 2015-03-09 19:15:16

+0

你好...现在我想我的loadpic.php工作正常 - 错误似乎是在插入脚本。你能看看insert.php并告诉我那里可能是错的吗?谢谢:)(我添加insert.php的问题) – 2015-03-12 16:10:17

+0

你传递7参数,而在绑定你有8个参数。你的第5个参数是图像,而你传递的是第5个参数的内容。你可以看看它是否你是对的? – 2015-03-12 17:40:04

0

试试这个。检查您是否获取插入值。如果插入并仍然出现错误,请发布与我以前的帖子相比较的数据库照片。

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

//run the query to insert the row 
$stmt->execute(array($uname, $typ, $titel, $content, $data, $mime, $timestamp)); 

header("Location: erfolg.php"); 
+0

好吧,看看这个问题,我添加了数据库的屏幕截图。顺便说一句,插入$数据的图像是正确的?或者我应该插入$ meta或$ image? – 2015-03-12 19:41:24

+0

您正在为图像内容分配$数据。所以你只需要为图片插入$数据。 $ meta是图片大小,$ image只是名称的参考。从您上次更新时,我只看到BLOB大小为5B。而我的输出给你5KiB。所以很明显,你没有上传图像内容。请处理我最新的代码并更新。 – 2015-03-12 19:46:52

+0

好的,我更新了它。但图像仍然无法显示。 – 2015-03-12 19:51:43

相关问题