Re: how 2 stroe images in Mysql Database using PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This one time, at band camp, David Grant <david@xxxxxxxxxxxx> wrote:


> Storing the image in the database will result in a fairly large
> performance hit

This is not always the case. Mostly this mis-nomer comes from using databases
that use a secondary db for indexing or dont support true BLOB types like pgsql.
I use MySQL to store images and did a few benchmarks and found it to be
faster if I stored the db on a RAW partition, thus eliminating any file system overhead.

The truth is that it is not slower, just folks dont know how to do it properly.

When using MySQL with the db stored on the file system I found a 1 second difference
of 100,000 accesses when benchmarked with ab. I also found this to be 0.5 seconds
faster than storing the image on the file system and the url in the database, this
was the _slowest_ method of access.

The db access is a small part of the script so the performance penalty does not propogate
linearly but remains constant so there is no exponetial slowdown.

Below is a quicky to get you started

Kind regards
Kevin

<?php
##########################################
###                                    ###
### This is a very basic file upload   ###
### script for my own purposes so      ###
### ymmv                               ###
###                                    ###
##########################################
                                                                                                                          
  // just so we know it is broken
  error_reporting(E_ALL);
?>
                                                                                                                          
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<h3>Basic upload of image to a database</h3>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile"  size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input type="submit" value="submit">
</form>
                                                                                                                          
<?php
  // check if a file was submitted
  if(!isset($_FILES['userfile']))
        {
        echo '<p>Please select a file</p>';
        }
  else
        {
        // upload the file
        upload();
        // give praise and thanks to the php gods
        echo '<p>Thank you for submitting</p>';
        }
                                                                                                                          
// the upload function
function upload(){
                                                                                                                          
  // include the config file
  include_once("config.php");
                                                                                                                          
  if(is_uploaded_file($_FILES['userfile']['tmp_name']))
        {
        // check the file is less than the maximum file size
        if($_FILES['userfile']['size'] < $maxsize)
                {
                // prepare the image for insertion
                $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
                // $imgData = addslashes($_FILES['userfile']);
                                                                           
                // get the image info..
                $size = getimagesize($_FILES['userfile']['tmp_name']);
                                                                                                                          
                // put the image in the db...
                // database connection
                mysql_connect("localhost", "$username", "$password") OR DIE (mysql_error());
                                                                                                                          
                // select the db
                mysql_select_db ("$dbname") OR DIE ("Unable to select db".mysql_error());
                                                                                                                          
                // our sql query
                $sql = "INSERT INTO testblob ( image_id , image_type ,image, image_size, image_name) VALUES ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";
                                                                                                                          
                // insert the image
                if(!mysql_query($sql))
                        {
                        echo 'Unable to upload file';
                        }
                }
        }
  else
        {
        // if the file is not less than the maximum allowed, print an error
        echo
        '<div>File exceeds the Maximum File limit</div>
        <div>Maximum File limit is '.$maxsize.'</div>
        <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
        <hr />';
        }
}
?>
                                                                                                                          
</body>
</html>
-- 
"Democracy is two wolves and a lamb voting on what to have for lunch. 
Liberty is a well-armed lamb contesting the vote."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux