Zend - The PHP Company




Databases

Add Code


Storing / Retrieving pictures from database  

Type: code fragment
Added by: imthiazrafiq
Entered: 23/07/2002
Last modified: 07/12/2001
Rating: - (fewer than 3 votes)
Views: 12382
Hi folks I have searched many sites for storing a picture into a database and retrieve the same. But I got some tutorials when I searched the web. Using the tutorials I have a made one for me. It was implemented using Php and Mysql. I will tell you want is the job of each files. 1). index.php This files links all the files in the project. 2). store.php This does the job of storing the picture into the database. 3). viewadd.php This file shows the entire picture stored in the database. 4). getpicture.php This file actually gets the file from file in the form of picture 5). mdbc.inc.php This is a class file for Mysql feel free to use. I got this over net. Here is the table description Id - > has the picture id Description -> has the picture description bin_data -> picture data filename -> picture file name filetype -> picture file type. Like gif or jpeg etc link -> you can store the link incase if you want to use this for ad exchange showno - > no of time ad show etc clickcount -> no of time the ad being clicked etc Query for the table. CREATE TABLE ads ( id varchar(15), description varchar(255), bin_data LONGBLOB, filename varchar(50), filesize varchar(50), filetype varchar(50), link varchar(255), shownno integer(5), clickcount integer(5), unique(id) ); If you want to see a site where I have implemented please have a look at the site http://www.asksathia.com/ Feel free to change anything in the code. If you developed a better version than this please send me the source code so that I may update myself. It took me much time when commenting the code of ease of understanding. Please vote for me. Regards Imthiaz Rafiq imthiazrafiq@gmx.net Msn id: imthiazrafiq@gmx.net Yahoo id: imthiazrafiq ICQ: 24294947 URL: http://high-flyer.da.ru


<HTML>
<HEAD><TITLE>Ads</TITLE>
</HEAD>
<BODY>
<?php
    
// code that will be executed if the form has been submitted:

    
if ($submit
    {
        
//please change the server name username and password according to your mysql server setting
        
$mysql_server="loaclhost";
        
$mysql_username="root";
        
$mysql_password="dbpass20";
        
$mysql_database="rnr";
        
//connect to database using above settings
        
@MYSQL_CONNECT("localhost",$mysql_username,$mysql_password);
        @
mysql_select_db("rnr");    
        
/* Add backslashes before characters that need to be quoted in database queries etc. 
           These characters are single quote ('), double quote ("), backslash () and NUL (the NULL byte). */
        
$data addslashes(fread(fopen($form_data"r"), filesize($form_data)));
        
//set the picture id a unix timestamp. I thought this will not have repeatations. You can keep anything what you like
        
$ads_id=time();
        
/*    Now insert the values into the table 
            oh sorry i forgot the mention the details of the table and here it goes. I have given the query to make ur work easier

                    CREATE TABLE ads
                    (
                        id varchar(15),
                        description varchar(255),
                        bin_data LONGBLOB,
                        filename varchar(50),
                        filesize varchar(50),
                        filetype varchar(50),
                        link varchar(255),
                        shownno integer(5),
                        clickcount integer(5),
                        unique(id)
                    );

        */
        
$result=MYSQL_QUERY("INSERT INTO ads (id,description,bin_data,filename,filesize,filetype,link,shownno,clickcount) ".
            
" VALUES ($ads_id,'$form_description','$data','$form_data_name','$form_data_size','$form_data_type','$form_link',$form_count ,$form_ccount)");
        
//print the id once the data is updated and this can be omited. Just incase....
        
print "<p>This file has the following Database ID: <b>$ads_id</b>";
        
//close the mysql database connection...
        
MYSQL_CLOSE();
    }
    else
    {
        
// else show the form to submit new data:
?>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td>Enter The Add Details</td>
          </tr>
        </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>
            <!--    Take a note this will work only if enctype in form is given as bellow since 
                    this is how it is done when submitting files in forms.    
            -->
              <form method="post" action="<?php echo $PHP_SELF?>" enctype="multipart/form-data" name="s">
                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                  <tr> 
                    <td>File Description:</td>
                    <td> 
                      <input type="text" name="form_description"  size="40">
                      <!-- this is to check is not over sized -->
                      <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
                    </td>
                  </tr>
                  <tr> 
                    <td>File to upload/store in database:</td>
                    <td> 
                      <input type="file" name="form_data"  size="40">
                    </td>
                  </tr>
                  <tr> 
                    <td>Link Of the ad</td>
                    <td> 
                      <input type="text" name="form_link" size="40">
                    </td>
                  </tr>
                  <tr> 
                    <td>Shown Count</td>
                    <td> 
                      <input type="text" name="form_count" size="40" value="0">
                    </td>
                  </tr>
                  <tr> 
                    <td">Clicked Count</td>
                    <td> 
                      <input type="text" name="form_ccount" size="40" value="0">
                    </td>
                  </tr>
                  <tr> 
                    <td>&nbsp;</td>
                    <td> 
                      <input type="submit" name="submit" value="submit">
                    </td>
                  </tr>
                </table>
                </form>
            </td>
          </tr>
        </table>
<?php
    
}
?>
</BODY>
</HTML>


Usage Example


See the example


Rate This Script





Search



This Category All Categories