how do I store a binary file in a MySql database, as a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence k

    how do I store a binary file in a MySql database, as a string?

    I'm a little weak on my basic I/O. Help me out please.

    Is it right to say that I can just open any file with file(), get it as
    a string, and then store in a MySql database, in, say, a MediumText
    field? I realize that MySql supports binary fields, but my current
    MySql schema does not have any binary fields, and I'm wondering if I
    can store the file without changing my current schema.

    Can I recreate the file later, if I wish, just be grabbing the string
    out of the database and writing it to disk? (on a Linux system)

  • Jerry Stuckle

    #2
    Re: how do I store a binary file in a MySql database, as a string?

    lawrence k wrote:[color=blue]
    > I'm a little weak on my basic I/O. Help me out please.
    >
    > Is it right to say that I can just open any file with file(), get it as
    > a string, and then store in a MySql database, in, say, a MediumText
    > field? I realize that MySql supports binary fields, but my current
    > MySql schema does not have any binary fields, and I'm wondering if I
    > can store the file without changing my current schema.
    >
    > Can I recreate the file later, if I wish, just be grabbing the string
    > out of the database and writing it to disk? (on a Linux system)
    >[/color]

    Yes.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Henrik Hansen

      #3
      Re: how do I store a binary file in a MySql database, as a string?

      "lawrence k" <lkrubner@geoci ties.com> writes:
      [color=blue]
      > I'm a little weak on my basic I/O. Help me out please.
      >
      > Is it right to say that I can just open any file with file(), get it as
      > a string, and then store in a MySql database, in, say, a MediumText
      > field? I realize that MySql supports binary fields, but my current
      > MySql schema does not have any binary fields, and I'm wondering if I
      > can store the file without changing my current schema.
      >
      > Can I recreate the file later, if I wish, just be grabbing the string
      > out of the database and writing it to disk? (on a Linux system)[/color]

      AFAIK you need to use blob fields as your data is binary (or is it,
      what kind of files do you want to open?), if you store
      it in a normal text field it can be corrupted. But else you are right,
      you just take the ourput from the field and put in a file or output to
      the browser with a header infront.


      --
      Henrik Hansen

      Comment

      • Alan Little

        #4
        Re: how do I store a binary file in a MySql database, as a string?

        Carved in mystic runes upon the very living rock, the last words of
        Henrik Hansen of comp.lang.php make plain:
        [color=blue]
        > "lawrence k" <lkrubner@geoci ties.com> writes:
        >[color=green]
        >> Is it right to say that I can just open any file with file(), get it
        >> as a string, and then store in a MySql database, in, say, a
        >> MediumText field? I realize that MySql supports binary fields, but my
        >> current MySql schema does not have any binary fields, and I'm
        >> wondering if I can store the file without changing my current schema.[/color]
        >
        > AFAIK you need to use blob fields as your data is binary (or is it,
        > what kind of files do you want to open?), if you store
        > it in a normal text field it can be corrupted. But else you are right,
        > you just take the ourput from the field and put in a file or output to
        > the browser with a header infront.[/color]

        You could base64_encode() it first.

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Henrik Hansen

          #5
          Re: how do I store a binary file in a MySql database, as a string?

          Alan Little <alan@n-o-s-p-a-m-phorm.com> writes:
          [color=blue]
          > Carved in mystic runes upon the very living rock, the last words of
          > Henrik Hansen of comp.lang.php make plain:
          >[color=green]
          >> "lawrence k" <lkrubner@geoci ties.com> writes:
          >>[color=darkred]
          >>> Is it right to say that I can just open any file with file(), get it
          >>> as a string, and then store in a MySql database, in, say, a
          >>> MediumText field? I realize that MySql supports binary fields, but my
          >>> current MySql schema does not have any binary fields, and I'm
          >>> wondering if I can store the file without changing my current schema.[/color]
          >>
          >> AFAIK you need to use blob fields as your data is binary (or is it,
          >> what kind of files do you want to open?), if you store
          >> it in a normal text field it can be corrupted. But else you are right,
          >> you just take the ourput from the field and put in a file or output to
          >> the browser with a header infront.[/color]
          >
          > You could base64_encode() it first.[/color]

          But then you have to keep in mind the content will take up much more
          space.

          Just tested, a 91959 bytes files will become 122612 bytes, that will end up
          being alot if you have lots of files..

          But it's a working solution if you cant convert to a BLOB (can't see
          why not though).

          --
          Henrik Hansen

          Comment

          • lawrence k

            #6
            Re: how do I store a binary file in a MySql database, as a string?


            Alan Little wrote:[color=blue]
            > Carved in mystic runes upon the very living rock, the last words of
            > Henrik Hansen of comp.lang.php make plain:
            >[color=green]
            > > "lawrence k" <lkrubner@geoci ties.com> writes:
            > >[color=darkred]
            > >> Is it right to say that I can just open any file with file(), get it
            > >> as a string, and then store in a MySql database, in, say, a
            > >> MediumText field? I realize that MySql supports binary fields, but my
            > >> current MySql schema does not have any binary fields, and I'm
            > >> wondering if I can store the file without changing my current schema.[/color]
            > >
            > > AFAIK you need to use blob fields as your data is binary (or is it,
            > > what kind of files do you want to open?), if you store
            > > it in a normal text field it can be corrupted. But else you are right,
            > > you just take the ourput from the field and put in a file or output to
            > > the browser with a header infront.[/color]
            >
            > You could base64_encode() it first.[/color]

            Thanks much to the both of you. I've MP3s to store. I will encode them
            with base64_encode() and store them in a mediumtext field. We will see
            how this goes.

            Comment

            Working...