storing files in a MySQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gellpak
    Member
    • Feb 2001
    • 54

    storing files in a MySQL database

    I get the impression that its possible, because i have seen vbb do it with avatars.
    how exactly is it done, and what are the pros/cons
    i would like to store images in a database, as well as be able to resize them into thumbnails automatically.
    any ideas?
  • lucaszero
    New Member
    • Jun 2001
    • 1

    #2
    I'd just store a URL to the file, and not the file itself.

    Comment

    • Gellpak
      Member
      • Feb 2001
      • 54

      #3
      yea, i can do that too, but i have specific applications for it this way

      Comment

      • Gellpak
        Member
        • Feb 2001
        • 54

        #4
        heh heh
        i figured it out, and its awesome
        thanks anyway

        Comment

        • Shoe
          Senior Member
          • May 2000
          • 488

          #5
          How is it done?
          "The best time to sleep is just before waking up."

          --Shoebuddy Jones

          Comment

          • Gellpak
            Member
            • Feb 2001
            • 54

            #6
            i used the vbb avatar systems as an example
            basically, you set up a form with "file" type input and enctype="text/multipart" in the form tag. then, the script takes that, inserts the various auto-created variables into the database, and outputs whatever.
            to get it is a simple script as well-- here they are:

            insert:
            PHP Code:
            <?
            // ********************************************************************************* //
            // | ECLIPSE IMAGE MANIPULATION SCRIPTS -- Add a pic to the database (pic-add.php) | //
            // | Copyright Adam Nelson, 2001AD                                                 | //
            // | VERSION: 1.0                                                                  | //
            // | LAST BUILD: June 5, 2001AD                                                    | //
            // |                                                                               | //
            // | site: www.neolantix.com                                                       | //
            // | email: [email protected]                                                  | //
            // |                                                                               | //
            // | These scripts may be used, modified, and distributed free of                  | //
            // | charge, under the concession that you do not remove these headers,            | //
            // | charge money for the use or purchase of MY scripts, or take credit            | //
            // | in any fashion for the creation or coding in these scripts.                   | //
            // | I would also very much appreciate a link back to my site :D                   | //
            // |                                                                               | //
            // ********************************************************************************* //

            // php/pic-add.php
            $thisfile $basefile;
            $thisfile .= "pic-add";

            $sectitle "Add Mod Screenshot";

            if (isset(
            $submit))
            {
               
            $size filesize($form_data);
               
            $image substr($form_data_type,0,5);
               
            $image strtolower($image);
               
            $length strlen($form_data_type);
               
            $start $length 3;
               
            $specifictype substr($form_data_type,$start,$length);


               if (
            $form_data == "none"
               {
                  
            $error "$error_pic_none";      
               } 
               else if (
            $size >= $maximgsize
               {
                  
            $error "$error_pic_toobig";
               } 
               else if (
            $image != "image"
               {
                  
            $error "$error_pic_type";
               } 
               else if ((
            $specifictype != "peg") && ($specifictype != "gif") && ($specifictype != "png"))
               {
                  
            $error "$error_pic_type";
               }
               else 
               {
                 
            $data addslashes(fread(fopen($form_data,  "r"), filesize($form_data)));
                 
            $checkpics "SELECT * FROM $modpictable WHERE modno=\"$modno\"";
                 
            $number_pics = @mysql_num_rows($checkpics);
                 
                 if (
            $number_pics >= $maxmodpics)
                 {
                    
            $error "$error_pics_toomany";
                 }
                 else
                 {
                      if ((!
            $comments) || ($comments == "none"))
                       {
                        
            $comments == "";
                     }
                     if ((!
            $modno) || ($modno "") || ($modno " "))
                     {
                        
            $modno == "0";
                     }
                    
                     
            $addpics "INSERT INTO $pictable (bin_data, filesize, filetype, modno, comment, filename)
                                 VALUES (\"
            $data\", \"$form_data_size\", \"$form_data_type\", \"$modno\", \"$comments\", \"$form_data_name\")
                                "
            ;
                     
            $addpicsresult mysql_query($addpics);
                     if (
            $addpicsresult
                     {
                         
            $img_data "SELECT picno FROM $pictable WHERE bin_data=\"$data\" AND filename=\"$form_data_name\"";
                    
            $img mysql_query($img_data);
                    while(
            $image mysql_fetch_array($img))
                    {
                        
            $id $image[picno];
                    }
                    
            // Insert the pic id number into the success message so that a valid link to the file 
                    // can be formed. Then, echo the success message.
                    
            $success_pic_add eregi_replace("id=""id=$id"$success_pic_add);
                    
            $msg "$success_pic_add";
                    
            $msg .= "<br>$backfooter";
                }
                  
                  }
               }
            }
            else
            {
               
            $msg "
            <form method=\"POST\" action=\"
            $thisfile\" enctype=\"multipart/form-data\">
              <center>
              Local file path:
              <input type=\"file\" name=\"form_data\" size=\"
            $browsesize\"><br><br>
              Comments:<br>
              <textarea rows=3 cols=
            $textareawidth name=\"comments\"></textarea><br>
              <input type=\"hidden\" name=\"modno\" value=\"
            $modno\">
              <input type=\"submit\" value=\"Submit\" name=\"submit\"><input type=\"reset\" value=\"Reset\" name=\"reset\">
              </center>
            </form>"
            ;
            }

            if (
            $error
            {
                
            $error .= $errorfooter;
            }
            $output "
                <font size=2><br>
                
            $error
                
            $msg
                </font>
            "
            ;
            $maincontent "$output";
            ?>
            and the get file script:
            PHP Code:
            <?php
            // ********************************************************************************* //
            // | ECLIPSE IMAGE MANIPULATION SCRIPTS -- Get a pic to the database (pic-get.php) | //
            // | Copyright Adam Nelson, 2001AD                                                 | //
            // | VERSION: 1.0                                                                  | //
            // | LAST BUILD: June 5, 2001AD                                                    | //
            // |                                                                               | //
            // | site: www.neolantix.com                                                       | //
            // | email: [email protected]                                                  | //
            // |                                                                               | //
            // | These scripts may be used, modified, and distributed free of                  | //
            // | charge, under the concession that you do not remove these headers,            | //
            // | charge money for the use or purchase of MY scripts, or take credit            | //
            // | in any fashion for the creation or coding in these scripts.                   | //
            // | I would also very much appreciate a link back to my site :D                   | //
            // |                                                                               | //
            // ********************************************************************************* //

            // php/pic-get.php

            // Connect to database ////////////////////////////////////////////////////////////////
            $mysqlconnection mysql_connect("$db_loc""$db_user""$db_path");
            if (
            $mysqlconnection
            {        
                
            $selectdatabase mysql_select_db("$db_name"$mysqlconnection);
            }

            // Select image data from database ////////////////////////////////////////////////////
            $img_data "SELECT bin_data,filetype,filename FROM pics WHERE picno=\"$id\"";
            $img mysql_query($img_data);
            while(
            $image mysql_fetch_array($img))
            {
                
            $filename $image[filename];
                
            $type $image[filetype];
                
            $data $image[bin_data];
            }

            // If the image was found, define its attributes and output it ////////////////////////
            if ($img)
            {
                
            Header "Content-disposition: filename=".$filename);
                   
            Header("Content-type: $type");
                   echo 
            $data;
            }
            ?>
            if you need help deciphering, ask specific questions and i will be happy to help out

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...