This is ancient, and I haven't tried it, but you may find it useful. 
---
/* Image Database Functions 

  by: J. Patrick Ryans [EMAIL PROTECTED] July 1998 

  The following are provided niether with copyright or warranty.  I do not 
  believe that any of the code in this file is copyrighted by anyone else, but 
  if it is, please let me know. 

  I gathered these functions from a larger php3 project to manage a 
  large and dynamic web site by keeping all html, images, and other file 
  contained in a MySQL database.  I couldn't find one single example that 
  explained how to insert binary images into a database and then extract them 
  and display it to the browser. 

  The two major functions needed to load and display images are shown below, 
  but I have left out many details such as how to connect to the database, 
  how to use html forms, etc.  That information can be picked up from a number 
  of places.  I.e. check out: 

  http://php.netvision.net.il/examples 

  for examples of how to connect to a database, and 

  http://liquid-sky.media.mit.edu/file_upload.html 

  for how to use forms to upload files 

  My database has a table for storing binary data such as images and other 
  types.  The schema is as follows: 

  # 
  # Table structure for table 'binary' 
  # 
  CREATE TABLE binary ( 
    id int(11) DEFAULT '0' NOT NULL auto_increment, 
    title varchar(200) DEFAULT '' NOT NULL, 
    auth_id varchar(16) DEFAULT '' NOT NULL, 
    description varchar(200), 
    category varchar(50) DEFAULT '' NOT NULL, 
    body longblob, 
    cr_date date, 
    datestamp timestamp(14), 
    type varchar(50) DEFAULT '' NOT NULL, 
    KEY category (category), 
    KEY title (title), 
    KEY auth_id (auth_id), 
    PRIMARY KEY (id) 
  ); 
  */ 


  /* Loading binary files into the database 

  There isn't room here to show the html forms, how to connect to the 
  database and the code for uploading the file into the database, so I'm 
  going to have to be a little terse here.  I'm going to assume that you 
  know how to use forms to upload files, and I'm going to assume that you 
  know how to connect to your database which has a table of structure 
  similar to the schema shown above. 

  This script is expecting to recieve the following variables from the form: 

  $title          // the title of the file 
  $auth_id        // the id name of the author -  field is specific to my needs 
  $desc           // a short description 
  $cat            // category, for organization purposes. 
  $type           // mime type - something like "image/gif" 

  Be forewarned, error checking is very minimal in this routine, you will 
  probably want to do some more sanity checking in a production environment. 
  In my forms, I query the database to present a select list of valid author 
  ids and mime types. 
  */ 
  <?  // load.php3 

          include  "admin_connect.ini";  // connect as a user with insert auth. 
          include  "main_h.inc";         // an html header for the page 
          echo  "<center>\n"; 


          $date = date( "Y-m-d"); 
           //$title = htmlentities($title);  // you may want to clean up the
input 
                                             // with statements like this. 

           // Check to see if a file was included in the input="file" tag 

          if(chop($fileinput)!= ""){ 

                   // $fileinput should point to a temp file on the server 
                   // which contains the uploaded image. so we will prepare 
                   // the file for upload with addslashes and form an sql 
                   // statement to do the load into the database. 

                  $image = addslashes(fread(fopen($fileinput, "r"), 1000000)); 
                  $SQL = 
  "Insert Into $PhotoTable
(title,auth_id,description,category,body,cr_date,type) values ('$title',
'$auth_id', '$desc', '$cat', '$image','$date','$type')";


                   // now we can delete the temp file 
                  unlink($fileinput); 
          } 
          else{ 
                  echo  "no file entered on form"; 
                  exit; 
          } 

          $Result = mysql_db_query ( $DB, $SQL ); 

          if($Result==0){ 
                  echo  "unsuccessful add"; 
          } 
          else{ 
                  echo  "successful add"; 
          } 
          include  "main_f.inc"; 
  ?> 

  /* Displaying an image from the database. 

  This is a very simple way to display the image that was loaded in the above 
  script.  In this case we will query the image by title, but it would be 
  a simple matter to add other queries. 

  You must have the mime type for the image stored in the record with the 
  image data.  As per the schema above, the mime type is stored in the 'type' 
  field. 

  Be careful that any includes and initialization files do not send anything 
  to the browser. You want the Header("Content-type: $type"); to be the first 
  line served. 

  To use the following script to display an image, use an image tag similar 
  to the following: 

  <img src="image.php3?title=myimage.gif"> 

  */ 
  <?  // image.php3 

           // connect to database with select access 
          include  "user_connect.ini"; 
          $SQL =  "select body,type from $PhotoTable where title='$title'"; 
          $Show = mysql ( $hDB, $hSQL ); 
          $Rows = mysql_num_rows($hShow); 

          if($Rows<1){ 
                   // no image matches this query 
          } 
          else{ 
                   // at least one image has this title 
                  $getPhoto = mysql_fetch_object($Show); 

                   // we need to determine the mime type 
                  $Type = $getPhoto->type; 

                   // and send the correct header to the browser 
                  Header( "Content-type: $Type"); 

                   // now send the image 
                  $Body = $getPhoto->body; 
                  echo $Body; 
                  flush(); 
          } 
  ?> 

Reply via email to