how to get an image from a database ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg Andersson

    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","x xxx") 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_resu lt = mssql_query($ge t_image) or die("Couldn't get
    image.");

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

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

    #2
    Re: how to get an image from a database ?


    "Georg Andersson" <georg.andersso n@swisstopo.ch>
    news:836ae599.0 406070437.4ba43 6ec@posting.goo gle.com...[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","x xxx") 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_resu lt = mssql_query($ge t_image) or die("Couldn't get
    > image.");
    >
    > $binary = @mssql_result($ get_image_resul t,0,"IMAGE_BIN" );
    > $name = @mssql_result($ get_image_resul t,0,"IMAGE_NAME ");
    >
    > header("Content-Disposition: attachment; filename=$name" );
    > header("content-Type: attachment; image/jpeg");[/color]


    Try this:

    $res=mssql_quer y("SELECT <field> FROM <table> WHERE <condition>")
    or die("SQL ERROR in line ".__LINE__. ", function mysql_query");
    $image=mssql_re sult($res, < tablename.field name >);
    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


    Comment

    • Georg Andersson

      #3
      Re: how to get an image from a database ?

      hi dennis

      tx a lot! the code works fine!

      best regards

      Comment

      Working...