0
votes

So I think I'll never get an answer lol ...

Old Question, please read the EDIT

I am trying to display a picture after uploading it to a mysql database.

But by trying to open the picture firefox shows the following warning:

the image cannot be displayed because it contains errors

I already had been searching google because of it for hours now but I really can't find a fix for this issue. So now you guys are my very last hope :)

Here is the 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;
    ?>

EDIT I think the mistake is in the insert.php.. Could someone please have a look at it and tell mewhat might be wrong? Thank you :) (I think the image is not uploaded correct)

<?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

2
Did u try to set the header in comment? This way u will see any warnings/errors the script is throwing. Be sure no warnings and errors are present before forcing the script as image - DarkBee
Are you sure the table from which you fetch the image is called artikel? - maljukan
@l0lander Yes I am :) It is the german meaning of article. - mm ibm
use print_r($row) to confirm whether you are retrieving the image. To display an image you need to point the location. Just name will display only the name. - Alaksandar Jesus Gene
oh, then i learned something already :) - maljukan

2 Answers

0
votes

I followed the instruction from here. I inserted the image with additional mime field and checked the code is working.

Confirm whether you have your mime types correct. If yes, then possibly the image might be corrupted.

having simply

header("Content-type: ");

also worked for me.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
votes

Try this. Check whether you are getting values inserted. If inserted and still getting error, post pics of database compared to my earlier post.

//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");