Serving Large Data From Files or From Blobs

Discussion in 'Perl Misc' started by FeelLikeANut@gmail.com, Feb 15, 2008.

  1. Guest

    My question is partly a language issue (memory) and partly a Web issue
    (where the program runs), so I hope everyone will be open to helping.

    Users may submit and store pictures with my program. I have the choice
    of storing these pictures either as files on the server or as a blob
    in the database. The pictures are just user data, and it seems logical
    to keep them in the database with all the other data. But I'm
    concerned about how to serve these pictures. I can write a program to
    handle image requests and return the appropriate picture from the
    database. But it seems to me this means the picture will need to be
    read from the database and stored in memory, in a variable, then
    written from the variable to the HTTP response.

    The part where the whole picture is stored in a variable is the part
    that worries me. Is this impractical? Or merely a bad idea? Is storing
    the pictures as files, allowing the server to serve them as it would
    server anything else, the best option?
    , Feb 15, 2008
    #1
    1. Advertising

  2. Guest

    wrote:
    > My question is partly a language issue (memory) and partly a Web issue
    > (where the program runs), so I hope everyone will be open to helping.
    >
    > Users may submit and store pictures with my program. I have the choice
    > of storing these pictures either as files on the server or as a blob
    > in the database. The pictures are just user data, and it seems logical
    > to keep them in the database with all the other data. But I'm
    > concerned about how to serve these pictures. I can write a program to
    > handle image requests and return the appropriate picture from the
    > database. But it seems to me this means the picture will need to be
    > read from the database and stored in memory, in a variable, then
    > written from the variable to the HTTP response.


    Databases that support blob types generally support ways to either stream
    those blobs or efficiently read them in chunks. How to do that would
    depend on the specifics of the database being used.

    How are you getting the data into the database in the first place without
    having it all in memory at once?

    > The part where the whole picture is stored in a variable is the part
    > that worries me. Is this impractical?


    It depends on the maximum practical size of the image, and the amount of
    memory your server has. Only you can know those things.


    > Or merely a bad idea? Is storing
    > the pictures as files, allowing the server to serve them as it would
    > server anything else, the best option?


    Do the file contents need the same transactional behavior (ACID, etc.)
    as the rest of the stuff in database?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Feb 15, 2008
    #2
    1. Advertising

  3. Rik Wasmus Guest

    On Fri, 15 Feb 2008 18:44:50 +0100, <> wrote:

    > My question is partly a language issue (memory) and partly a Web issue
    > (where the program runs), so I hope everyone will be open to helping.
    >
    > Users may submit and store pictures with my program. I have the choice
    > of storing these pictures either as files on the server or as a blob
    > in the database. The pictures are just user data, and it seems logical
    > to keep them in the database with all the other data. But I'm
    > concerned about how to serve these pictures. I can write a program to
    > handle image requests and return the appropriate picture from the
    > database. But it seems to me this means the picture will need to be
    > read from the database and stored in memory, in a variable, then
    > written from the variable to the HTTP response.
    >
    > The part where the whole picture is stored in a variable is the part
    > that worries me. Is this impractical? Or merely a bad idea? Is storing
    > the pictures as files, allowing the server to serve them as it would
    > server anything else, the best option?



    Insertion & retrieving is very much streamlined (huhu, pun not intended,
    but it indeed uses streams) with PDO, look at the Large Objects (LOB)
    example at <http://nl.php.net/pdo>:

    Insertion:
    <?php
    $db = new PDO();//put your connections variables here
    $stmt = $db->prepare("INSERT INTO tablename (image) values (?)");
    $fp = fopen($_FILES['file']['tmp_name'], 'rb');
    $stmt->bindParam(1, $fp, PDO::pARAM_LOB);
    $stmt->execute();
    fclose($fp);
    ?>

    Retrieval:
    <?php
    $db = new PDO();//put your connections variables here
    $stmt = $db->prepare("SELECT image FROM tablename WHERE id = ?");
    $stmt->bindParam(1,$_GET['id'],PDO::pARAM_INT);
    $stmt->execute();
    $stmt->bindColumn(1, $lob, PDO::pARAM_LOB);
    $stmt->fetch(PDO::FETCH_BOUND);
    header("Content-Type: image/png");//or other format...
    fpassthru($lob);
    ?>
    --
    Rik Wasmus
    Rik Wasmus, Feb 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eqbal Z
    Replies:
    10
    Views:
    1,309
    Andrew Thompson
    May 22, 2004
  2. Tim Stone

    Manipulating large blobs in Python

    Tim Stone, Apr 22, 2005, in forum: Python
    Replies:
    0
    Views:
    309
    Tim Stone
    Apr 22, 2005
  3. Replies:
    2
    Views:
    293
  4. Tool69
    Replies:
    10
    Views:
    503
    Martin v. Löwis
    May 25, 2008
  5. Srinidhi

    Serving dynamic images and files

    Srinidhi, Apr 30, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    Srinidhi
    Apr 30, 2009
Loading...

Share This Page