Uploading & Retrieving Images From Database

Associate
Joined
29 May 2005
Posts
144
Hey Everyone :rolleyes:

I have been working on adding a retreiving images from a database, and stumbled accross this method using BLOB which I am currently trying to implement into my website.

I have the following upload code, to add the image to my SQL database, but am having problems displaying the images, and am not sure if this is the best way to do things.

Here is my database structure with a couple images in it :eek:

db11.JPG


1) Is this the best way to store the images, or should I simply be storing the URL in a varchar instead and calling that to the page? If so how can I code it so when the user uploads a file it adds that path to the database? That confused me.

2) If this is the best method to do it, how on earth do I display an image using this method? I have tryed and tryed but have come no where near close to displaying anything like an image.

Any help would be greatly appreciated.

Thanks again - Mofish!

upload.php
Code:
<?

if (!isset($_SESSION['member'])) {
echo "<div class='error-style'>please login first</div>";
}else{

if(isset($_POST['upload']))
{
	echo 'Please Wait ... Processing Your Upload';
		
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        
        $fp = fopen($tmpName, 'r');
        $content = fread($fp, $fileSize);
        $content = addslashes($content);
        fclose($fp);
        
        if(!get_magic_quotes_gpc())
        {
            $fileName = addslashes($fileName);
        }
        
        $query = "INSERT INTO upload (name, size, type, content ) ".
                 "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

        mysql_query($query) or die('Error, query failed');                    
        
        echo "<div>File <b>$fileName<b> Uploaded Successfully</div>";
}        
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
      <td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="2000000" class="loginbox" /></td>
    </tr>
    <tr>
      <td colspan="2">
	  	Max File Size: <b>20mb</b>		
		<br />
        Supported Files: <b>All</b>
		<br />
      </td>
    </tr>
    <tr> 
      <td width="218">
	  <br />
	  	<input name="userfile" type="file" id="userfile" class="loginbox">      
	  </td>
      <td width="125">
	  <br />
	  	<input name="upload" type="submit" id="upload" value="Upload" class="loginbox">
	  </td>
    </tr>
  </table>
</form>
<?
}
?>
 
generally storing images in a database is a nono. Loading files from your OS filesystem is always going to be faster and less server intensive than expensive database calls.

just create an uploads directory and store just the filename in your database. the only drawback with this is that you'll need to devise a way of checking for duplicate file names :)
 
Back
Top Bottom