Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 06:41 AM
Georg Andersson
Guest
 
Posts: n/a
Default 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");
  #2  
Old July 17th, 2005, 06:41 AM
Dennis Biletsky
Guest
 
Posts: n/a
Default Re: how to get an image from a database ?


"Georg Andersson" <georg.andersson@swisstopo.ch>
news:836ae599.0406070437.4ba436ec@posting.google.c om...[color=blue]
> 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");[/color]


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


  #3  
Old July 17th, 2005, 06:42 AM
Georg Andersson
Guest
 
Posts: n/a
Default Re: how to get an image from a database ?

hi dennis

tx a lot! the code works fine!

best regards
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles