By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

how to get an image from a database ?

P: n/a
hi

i'm using a liitle script to insert images in a mssql database. yes i
know i'm not supposed to store images in a database, but after my
images are only 3kb in size, i've decided to do it anyway.

question: how would it be possible to get one of this images back on
my disc? i've been looking through many posting in different forums.
but all of them are hard to understand if you don't have any
experiences using php. the code below is as far as i could get by now.
but the downloaded file is only 318 bytes in size and can not be
displayed. so obviously, my code is crep! but what am i doing wrong?
2. question: can a file be downloaded without prompting the user in
advance? the whole application i'writing is for inhouse use only and
therefor, the user should trust these files...

any help would greatly appreciated!!

best regards


$db = mssql_connect("xxxx","xxxx","xxxx") or die("Can't connect to
mssql server.");
mssql_select_db("xxxx", $db) or die("Can't select database.");

$get_image = "SELECT IMAGE_BIN,IMAGE_NAME FROM dbo.IMAGE WHERE ID_GCP
=51 And ID_LEVEL_IMG=2";
$get_image_result = mssql_query($get_image) or die("Couldn't get
image.");

$binary = @mssql_result($get_image_result,0,"IMAGE_BIN");
$name = @mssql_result($get_image_result,0,"IMAGE_NAME");

header("Content-Disposition: attachment; filename=$name");
header("content-Type: attachment; image/jpeg");
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Georg Andersson" <ge*************@swisstopo.ch>
news:83**************************@posting.google.c om...
hi

i'm using a liitle script to insert images in a mssql database. yes i
know i'm not supposed to store images in a database, but after my
images are only 3kb in size, i've decided to do it anyway.

question: how would it be possible to get one of this images back on
my disc? i've been looking through many posting in different forums.
but all of them are hard to understand if you don't have any
experiences using php. the code below is as far as i could get by now.
but the downloaded file is only 318 bytes in size and can not be
displayed. so obviously, my code is crep! but what am i doing wrong?
2. question: can a file be downloaded without prompting the user in
advance? the whole application i'writing is for inhouse use only and
therefor, the user should trust these files...

any help would greatly appreciated!!

best regards


$db = mssql_connect("xxxx","xxxx","xxxx") or die("Can't connect to
mssql server.");
mssql_select_db("xxxx", $db) or die("Can't select database.");

$get_image = "SELECT IMAGE_BIN,IMAGE_NAME FROM dbo.IMAGE WHERE ID_GCP
=51 And ID_LEVEL_IMG=2";
$get_image_result = mssql_query($get_image) or die("Couldn't get
image.");

$binary = @mssql_result($get_image_result,0,"IMAGE_BIN");
$name = @mssql_result($get_image_result,0,"IMAGE_NAME");

header("Content-Disposition: attachment; filename=$name");
header("content-Type: attachment; image/jpeg");

Try this:

$res=mssql_query("SELECT <field> FROM <table> WHERE <condition>")
or die("SQL ERROR in line ".__LINE__.", function mysql_query");
$image=mssql_result($res, < tablename.fieldname >);
header("Content-type: image/gif");
echo $image; // image is output to browser
if (!$handle = fopen($filename, 'w')) {
print "Cannot open file ($filename)";
exit;
}
fwrite($handle, $image);
fclose($handle); // image is output to disc
Jul 17 '05 #2

P: n/a
hi dennis

tx a lot! the code works fine!

best regards
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.