Adding the images to database MySQL by means of PHP

Adding the images to database

First of all I’d like to note that in order to store images in the database MySQL it is necessary to specify one of the fields of the table as a derivative from the type BLOB. Abbreviation BLOB means large binary object.  Pattern of storage of BLOB data can have several options:

  • TINYBLOB can store up to 255 bytes
  • BLOB can store up to 64 kilobytes of information
  • MEDIUMBLOB up to 16 megabytes
  • LONGBLOB up to 4 gigabytes

To store the Image file in the database, it is necessary to read the file in variable and to create the query to add the data to the table. In my case the task was to add two images to the base by means of the form with the use of PHP. We have the form with two fields and button “Send”:

Each image should not exceed 1Mb!

I should remind that the attribute action indicates the file, which will download the image file.
The attribute enctype indicates the coding method of the content of the form and the information about the file uploads.

Note: Support of download of several files was introduced in the version 3.0.10.
Since we sendtwo files in the attribute name after the word we need to indicate “userfile[]” in square brackets; this means that we send several files with the use of themassive in which the file attributes are:

Original file name on the client.
mime is a file type if the browser gave this information. Example: "image/gif".
Temporary file name under which the downloaded file was saved on the server. How to obtain the values of each file? For instance, let’s suppose that files named /home/test/1.jpg/home/test/2.jpg are sent. In this case $_FILES['userfile']['name'][0] will contain the value 1.jpg, а $_FILES['userfile']['name'][1] – value 2.jpg. In the same way, $_FILES['userfile']['size'][0] will contain the value of the size of the file 1.jpg, and so on.

Now let’s consider the file code add_image.php, which was indicated in the attribute of the form action.

$ErrorDescription = '';

function get_image($num){

    global $ErrorDescription;

    static $image_size=0;

    // Check if the global variable is not empty $_FILES



       //File size restriction; in my case 1Mb

         $ErrorDescription="Each image should not exceed 1Mb! 
        Otherwise the image can not be added to the database.";
         return '';

    // If the global variable $_FILES is not empty, 
    // we need to check if this file is image one or not (out of security reason)
    if(substr($_FILES['userfile']['type'][$num], 0, 5)=='image'){
        // Read the content of the file 
        // Screen the special symbols in the file content 
        return $image;
      $ErrorDescription="You have downloaded not an image, so it cannot be added.";
      return '';

       $ErrorDescription="You have not downloaded the image, the field is empty, so the file cannot be added to the database.";

         return ;


    return $image;



//With the use of the function specified in advance get_image we attribute
//variables the file content 

if($image_before == ''){unset($image_before);}else{$image_after=get_image(1);
if ($image_after == '') {unset($image_after);}}

//Let’s check if the variables are set 

if(isset($title) && isset($image_after) && isset($image_before)){
    // Here we write that information can be added to the database.  
    // In our case in the database there are two fields of the type     
    // MEDIUMBLOB: img_before and img_after

    $result = mysql_query ("INSERT INTO imagess (title,img_before,img_after) VALUES ('$title','$image_before','$image_after')");

    if ($result == 'true'){
        echo "Your images are added!";
    } else {
       echo " Your images are not added!";


    if ($ErrorDescription == ''){
        echo "You have note entered all the information, so the images can not be added to the database.";
        echo $ErrorDescription;


So in this narticle we discussed how to save the image in the database MySQL with the use of PHP. In the second article we will discuss how to extract the saved image file from the database.