Jump to content

[SOLVED] display image from mysql - 'Image Contains Errors'


Hyaku_

Recommended Posts

Hi!
I'm trying to figure out, how to display image from mysql database, but Firefox says that image contains errors.

this is the image forms action page, When I check into database, image is actualy saved there:
[code]$img = $_FILES['img']['tmp_name'];
$img_size = filesize($img);
$img_data = addslashes(fread(fopen($img, "r"), $img_size));

$query = "UPDATE USER_ACCOUNT SET IMG = '$img_data' WHERE ID = 1";
$query_exec = mysql_query($query);[/code]

img.php:
[code]$query = "SELECT IMG FROM USER_ACCOUNT WHERE ID = 1";
$query_exec = mysql_query($query);
list($img) = mysql_fetch_array($query_exec);

Header("Content-Type: image/jpeg");
echo stripslashes($img);
[/code]

index.php:
[code]echo "<img src='img.php'>";[/code]
but I get 'The image contains errors..' message. Any suggestions? Thank you!
Link to comment
Share on other sites

[quote author=jesirose link=topic=121602.msg500405#msg500405 date=1168318279]
He is echoing it. In order to actually see it as the text instead of an image, you'll need to include the page img.php using include or require in addition to doing the img src. This will help you debug.

[/quote]

A.) I meant echo just the database value not within the img src tags so u can see the actual values stored, guess i should have specified that

and B.) try putting this for your $img =  on the page that u upload the pics from

[code]$img = basename($_FILES['img']['tmp_name']);[/code]
Link to comment
Share on other sites

Thanks!
I added basename, and here are contents of the file in database:
EDIT: Sorry, I mean hear are just a fiew chars, theres tons of random characters, but for some reason copy/paste doesn't quite work for me here..
[code]ÿØÿà JFIF..[/code]
It is stored into database and that looks like JPEG header, I try to upload gif and change the header(.. image/gif); but still the same thing.. ???
Link to comment
Share on other sites

Lemme give u my working code real quick:

[code]<?php


$file_upload = "true";

if ($imgfile_size > 125999)
{
$msg="Your uploaded file must be less than 125kb.<BR>";
$file_upload="false";
}

if (!($imgfile_type=="image/jpeg" || $imgfile_type=="image/gif"))
{
$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}




if($file_upload=="true") {


$uploaddir = 'thumbs/';
$uploadfile = $uploaddir . basename($_FILES['imgfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile)) {

$img_size = filesize($imgfile);
$img_data = addslashes(fread(fopen($imgfile, "r"), $img_size));

$query = "UPDATE USER_ACCOUNT SET IMG = '$img_data' WHERE ID = 1";
$query_exec = mysql_query($query);

  echo "File upload was successful.<br><br><a href=\"$uploadfile\">View: $uploadfile</a>\n";
} else {
  echo "File upload failed.\n";
}

}

if($file_upload=="false") {
echo" <h2>Error:</h2><br><br>
$msg";
}



?>


[/code]

oops, i forgot to modify that to work with ur database, one moment please
Link to comment
Share on other sites

The code I posted above should give you a good start..may be alittle buggy cuz I haven't tested it like that but it should put it in your database...you may need to rename some variables and such though cuz mine are different than yours...Oh also I have it set to limit file size to 125k I think..u can easily change that.
Link to comment
Share on other sites

Thanks man!
Could that be posible if I use function addslashes() when inserting data into mysql and then reading the information and using stripslashes(); these two functions lose some special characters like [b]"[/b], [b]'[/b] etc.. from image?
Link to comment
Share on other sites

The purpose of addlsahes is to escape characters like ' and " making them \' and \" so that the database can take them in in the query without error.  stripslashes should in turn take the \ back out and make them ' and " again if those characters are in your files...so in theory when you pull the file back out of the database and stripslashes before calling it in the img src tag it should work fine...although i would test echoing the data with and without stripslashes without the img src tag if you have any errors in the img tags
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.