Adding the images to database MySQL by means of PHP

Adding the images to database
image_pdfimage_print

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:

$_FILES['userfile']['name']
Original file name on the client.
$_FILES['userfile']['type']
mime is a file type if the browser gave this information. Example: "image/gif".
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']
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

    if(!empty($_FILES)){

        $image_size=$_FILES['userfile']['size'][$num];

       //File size restriction; in my case 1Mb

      if($image_size>1024*1024||$image_size==0){
         $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 
        $image=file_get_contents($_FILES['userfile']['tmp_name'][$num]);
        // Screen the special symbols in the file content 
        $image=mysql_escape_string($image);
        return $image;
    }else{
      $ErrorDescription="You have downloaded not an image, so it cannot be added.";
      return '';
    }
}else{     

       $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 
$image_before=get_image(0);

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!";
    }

}else{

    if ($ErrorDescription == ''){
        echo "You have note entered all the information, so the images can not be added to the database.";
    }else{
        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.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *