Jump to content

image retrieving from mysql problem


strazdinjsh

Recommended Posts

That must be very simple but i can not step over the issue what i have got.

1. i have file which displays the image called index.php with source code of

--------------------------------------------------------------------

...

<img src="sample.php?id=42" />

...

--------------------------------------------------------------------

2. then i have file sample.php with source code

-------------------------------------------------------------------------------------

require("sys/functions.php");

dbConnect();

$id = $_GET["id"];

if(!isset($id))

{

echo "select an ID";

}

else

{

$res = mysql_query("SELECT * FROM gallery where id=$id");

$row = mysql_fetch_assoc($res);

 

header("Content-type: image/$row[extension]");

echo $row['image'];

}

-------------------------------------------------------------------------------------------

3. i have successfully uploaded file to MySQL, i can see it as a record with id=42 (image - that is the actual file, extension - image extension)

4. when launching the index.php i am not getting an image but still can get a file size, approx. must be right 248kb. image itself does not display

 

Could anyone have a look and give me some idea to start with... thanks a lot.

Link to comment
Share on other sites

DB field does not contain name and the path to the image because image is located in DB itself. It is not the name of file what is kept in table but image. Is that an answer on your question? if i am wrong, correct me please.

 

P.S. I used to work with mysql and usually kept the path to the image, there were no problems. I am trying to keep images in DB instead of web root folders

Link to comment
Share on other sites

What exactly does $row[extension] contain, because there are only a few Content-type: image/ values where the actual file extension IS a valid Content-type?

 

What do you get when you browse directly to sample.php?id=42 ??? If you get a blank screen when you do that, what does a 'view source' in the browser show?

 

Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed?

Link to comment
Share on other sites

$row[extension] holds the extension of the file and in this case is jpg, it is necessary to define what file output will be expected - picture (png, gif, jpeg, jpg etc.) or pdf or... I am getting image approx 50 x 50 which usually showed when image can not be found or just output. View Source is disabled, i can not look inside the source generated by browser. Opera lets check the file size - approx 259KB. Yes, E_ALL is set and display errors is ON in php.ini

Link to comment
Share on other sites

While it might not be the only problem preventing your image from working, as already stated, the content type for a .jpg is not image/jpg, it's image/jpeg

 

Edit: If your browser indicates the size of the output is the expected value, it is likely that using the correct Content-type: will fix the problem.

 

Link to comment
Share on other sites

Temporarily comment out the header() statement and add the following two lines right after your first opening <?php tag in sample.php -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

 

I also recommend that you post the code that uploaded and inserted the file into the database.

Link to comment
Share on other sites

P.S. I used to work with mysql and usually kept the path to the image, there were no problems. I am trying to keep images in DB instead of web root folders

 

There are more options than these 2. I can't think of any good reason to keep binary files in a database. But... I have been wrong before.

Link to comment
Share on other sites

With the following code in sample.php, what do you get when you browse directly to sample.php?id=42

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
require("sys/functions.php");
dbConnect();
$id = $_GET["id"];
if(!isset($id))
{
echo "select an ID";
} else {
$res = mysql_query("SELECT * FROM gallery where id=$id");
$row = mysql_fetch_assoc($res);

// header("Content-type: image/$row[extension]");
echo $row['image'];
}
?>

Link to comment
Share on other sites

sorry, i did not reply on your previous comment correctly. I did not get any result with your suggestions when launched index.php (source code showed ..... <img src="sample.php?id=42" />.......). When i launched sample.php?id=42 i got something like binary code of the picture output. Unfortunately i could not get my machine with apache on it at work, so i can not double check it. I will get back as soon as i will be able to. Thank you for your time and efforts.

 

P.S. i will try to use other record with picture, might be problem is hiding in the file or extension or.. or..

Link to comment
Share on other sites

image insertion below (DB table gallery with fields - id(int, auto increment), image(longblob), extension(varchar), date_time(timestamp))

require("sys/functions.php");
dbConnect();
                $data = file_get_contents("img/test.jpg");
                $data = mysql_real_escape_string($data);
                mysql_query("INSERT INTO gallery SET image='$data'");

 

image output below

 

require("sys/functions.php");
dbConnect();


	$id = $_GET["id"];

	if(!isset($id))
	{
	echo "select an ID";
	}
			else
			{
			$res = mysql_query("SELECT * FROM gallery where id=$id"); 
			$row = mysql_fetch_assoc($res);
                                $im = $row["image"];
  				header("Content-type: image/jpeg");
    			echo $im;
    			}

Link to comment
Share on other sites

I apologize for the insertion code - there is an extra field [extension] in table who was added manually afterwords when dealing with issue. Field info added manually as [jpg] and tried [jpeg] and meant to use for content output. In output source code i removed content output detection and set it manually to [jpeg]

 

 

Link to comment
Share on other sites

There are at least 3 things that could prevent what you are doing from working -

 

1) Something you have in your image output code that you have not shown in the post, such as using a short open tag or intentionally echoing something before the code you have shown, is preventing the code from being seen as code or is preventing the content-type header from working. Could you post the WHOLE contents of that file? Your "sys/functions.php" file or your dbConnect(); function could also be causing some inadvertent output to the browser that is preventing the content-type header from working.

 

2) Your image output code file could have the Byte Order Mark (BOM) characters at the start of the file (put there by your editor when the file is saved for a UTF-8 encoded file) and the content type header is not working. The display_errors/error_reporting settings that have been suggested is something you should always have set as suggested when developing and DEBUGGING php code and would point out if there are any problems that php can detect that would help solve the problem. Having the display_errors/error_reporting settings set as suggested would also help find if there is any other output, like mentioned in item #1 above, that is preventing the content-type header from working.

 

3) magic_quotes_runtime could be on which would cause your image data to be double escaped and invalid when you retrieve it and output it. What does the following php code show for the magic_quotes_runtime setting -

<?php
var_dump(get_magic_quotes_runtime());
?>

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.