On 04.04.2001 08:55:04 Nicolas Villatte wrote:

> is it possible? If yes, how ?

Could someone *PLEASE* add this to the FAQ?

There are two answers you'll get:

a) Don't store the binary data in the database.  Instead store nothing but the
metadata.
b) Also store the binary data in the database.

Reasons:
a) The filesystem is the best binary database there is.  Accessing the
filesystem to retrieve binary data is possibly faster than to do it thru MySQL.
b) It's more convenient to have all the data in the database instead of
spreading it all over.

Here's how I do b) in PHP:

1) Get the MIME type of the image.
2) Read the binary file contents and store it in a PHP variable.
3) Base64 encode the binary data, so that there is no binary anymore, but just
plain ASCII data.
4) Store the image in a MEDIUMTEXT or LONGTEXT column
5) Store the MIME type

When I want to retrieve and display the image, I do:

1) Retrieve the MIME type
2) Retrieve the Base64 encoded binary
3) Decode the base64 ascii to get the binary
4) Print the MIME type in the header (if we're talking about HTML, that is)
5) Print the image

Code:

/*
BinTable contains at least three columns:
 ID  -> autoincrementing int
 MIME     -> VARCHAR( 50 )
 Bin -> MEDIUMTEXT
*/

function StoreImage( $MIME, $Binary ){
  // Store the binary with the associated MIME type in a database
  // Returns the row id of the just inserted row

  $base64 = base64_encode( $Binary );
  $q  = "INSERT INTO BinTable ( MIME, Bin ) VALUES (";
  $q .= "'" . $MIME . "'";
  $q .= ", '" . $base64 . "'";
  $q .= ")";

  mysql_query( $q );
  return( mysql_insert_id() );
}

function PrintImage( $ID ){
  // Retrieve the image with the assoc. ID from the BinTable table
  // Returns TRUE if everything was Okay; FALSE otherwise
  // Sideeffect: Prints the image with header to stdout

  $q  = "SELECT MIME, Bin FROM BinTable WHERE ID = " . $ID;
  $rs = mysql_query( $q );
  if ( mysql_num_rows( $rs ) ){
    // There is something to return
    if( ! headers_sent() ){
      // Headers have not yet been sent - we can go ahead
      $row = mysql_fetch_object( $rs );
      $bin = base64_decode( $row->Bin );
      header( "Content-Type: " . $row->MIME );
      print $bin;
      $ret = TRUE;
    } else {
      // Headers have already been printed - cannot continue
      $ret = FALSE;
    }
  } else {
    $ret = FALSE;
  }
  return( $ret );
}

// The following assumes, that there is a <INPUT TYPE=FILE NAME=image> in the
HTML form
// Futher, there also needs to be a form element where the MIME Type is
specified
// I assume <SELECT NAME=MIME><OPTION VALUE="image/png">png</SELECT>
if( is_uploaded_file( $image['tmp_name'] ) ){
  // The file has been uploaded - safe to store in database

  // Get the size in bytes of uploaded file
  clearstatcache();
  $fs = filesize( $image['tmp_name'] );

  // Read file contents into a variable
  $fd = fopen( $image['tmp_name'], "r" );
  $file_bin = fread( $fd, $fs );

  // Close the file
  fclose( $fd );

  // Store the image in the database
  $img_ig = StoreImage( $MIME, $file_bin );

  // And also print it
  if( ! PrintImage( $img_id ) ){
    echo "For some reason the image could not be printed!";
  }
}



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to