iic has asked for the wisdom of the Perl Monks concerning the following question:

Hi,Perl monks!
I know I can use Win32::ODBC to operate the DSN of
SQLServer on NT OS.
Now I want to save a GIF that user upload from a FORM into
a table in the database as a item.
My question is How do I insert GIF into SQLServer and
How to load the GIF and Print it on a web page?
insert sometable(Name,Image)#?????????????
values('john','/directory/john.gif')#??????????????
Many thanks.

Replies are listed 'Best First'.
Re: insert GIF into SQLServer
by Corion (Patriarch) on Jul 21, 2000 at 13:55 UTC

    Most of the time, it's a bad idea to insert large data like pictures into a database, as SQL is not really standardized about how big BLOBs (== binary large objects or something like that) are allowed to become.

    I suggest that you create a unique filename for your image and then save the uploaded image on the disk and a link to that image in your database.

    If you really really really need to store your images in the database (and your images are all guaranteed to be smaller than 4k, because many databases have problems with larger entries), you will have to encode your images to make them SQL-safe and then store them like any other value. Why making them SQL-safe ? You wouldn't want a user to upload a cleverly crafted image that executes some valid SQL code on your database, for example dumping all password data.

    But in general I think it's a better solution to store the files themselves out of the database.

    I just saw that you will be using Win32::ODBC to access your database - be prepared for even weirder limits on field size, as now both the ODBC driver and the database impose limits on your data.

Re: insert GIF into SQLServer
by JP Sama (Hermit) on Jul 21, 2000 at 17:57 UTC
    You could use the pack/unpack idea, mentioned by BBQ!!
    Well, good luck!

    =)

    #!/jpsama/bin/perl -w
    $tks = `mount`;
    $jpsama = $! if $!;
    print $jpsama;
    
Re: insert GIF into SQLServer
by mrmick (Curate) on Jul 21, 2000 at 18:58 UTC
    Why not have your GIF image uploaded to the server and have the database store the path? This way, you will be able to use the database to retrieve the path of the GIF and embed the string into the IMG SRC= part of the page.
    Mick