Re: Antwort: Storing pictures in mySQL

2001-04-04 Thread Peter Skipworth

Ouch! I don't know if many people would recommend this method - that's a
fair bit of unnecesary overhead to place ony our server...depending, of
course, on how often these images are created/modified/read.

P


 
 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=MIMEOPTION 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
 
 


-
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




Re: Antwort: Storing pictures in mySQL

2001-04-04 Thread Johan Andersson

You may look at the MySQL related code in phpAds from phpwizard.net:
http://www.phpwizard.net/projects/phpAds/

It will bring you all you need in code, but I've tested that system on a website
with 30 concurrent users with 2 ads (2 image SELECTs) per pageview and
that was a bg performance buster.

So, I still recommend you to place the image files in the filesystem and only
the filenames in the database. (I use the PK for that table as filename, then you
don't need to store the filenames either... )


Regards,
Johan Andersson
Consultant

- Original Message - 
From: "Peter Skipworth" [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Cc: "Nicolas Villatte" [EMAIL PROTECTED]; "Mysql (E-mail)" 
[EMAIL PROTECTED]
Sent: Wednesday, April 04, 2001 11:21 AM
Subject: Re: Antwort: Storing pictures in mySQL


 Ouch! I don't know if many people would recommend this method - that's a
 fair bit of unnecesary overhead to place ony our server...depending, of
 course, on how often these images are created/modified/read.
 
 P
 
 
  
  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=MIMEOPTION 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
  
  
 
 
 -
 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
 
 


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

To r

Re: Antwort: Storing pictures in mySQL

2001-04-04 Thread alexander . skwar


On 04.04.2001 11:21:46 Peter Skipworth wrote:

 Ouch! I don't know if many people would recommend this method - that's a
 fair bit of unnecesary overhead to place ony our server...depending, of
 course, on how often these images are created/modified/read.

Do you mean the base64 part, or what?  Or are you talking about storing it in
the database at all?

BTW: Is your email adress correct?  It got here as:
[EMAIL PROTECTED]  This looks strange.



-
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