467,104 Members | 1,007 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

how to get an image from a database ?

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
  • viewed: 2268
Share:
2 Replies

"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
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.

Similar topics

6 posts views Thread by bissatch@yahoo.co.uk | last post: by
6 posts views Thread by Rudy | last post: by
35 posts views Thread by Stan Sainte-Rose | last post: by
7 posts views Thread by lgbjr | last post: by
3 posts views Thread by den 2005 | last post: by
7 posts views Thread by eholz1 | last post: by
10 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.