Skip to content Skip to sidebar Skip to footer

Php Display Preview Image Upload to Database

Y'all can save your uploading images in the database table for later use e.thou. display user profile or product paradigm, create the epitome gallery, etc.

There are 2 ways of doing this –

  • Salvage the path or proper name of an image
  • Encode epitome into a base64 format

In this tutorial, I show you lot both of the methods for storing and retrieving an image from the database table.

Upload and store an image in the Database with PHP


Contents

  1. Tabular array structure
  2. Configuration
  3. Relieve path or name
  4. base64_encode()
  5. Determination

1. Table structure

In the example, I am using images table for storing data.

  • name – This field is used to store the epitome file name.
  • epitome – This field is used to shop the paradigm base64 generated value.
CREATE Tabular array `images` (   `id` int(eleven) Non Cipher PRIMARY Central AUTO_INCREMENT,   `proper name` varchar(200) Non Zilch,   `image` longtext NOT Nothing ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ii. Configuration

Create a new config.php file for database configuration.

Completed Code

<?php  $host = "localhost"; /* Host proper name */ $user = "root"; /* User */ $countersign = ""; /* Countersign */ $dbname = "tutorial"; /* Database name */  $con = mysqli_connect($host, $user, $password,$dbname); // Check connection if (!$con) {   die("Connection failed: " . mysqli_connect_error()); }

three. Save path or name

Yous can either save the full path or proper name of an paradigm in your MySQL database table. Retrieve the paradigm proper name or path from the MySQL database and utilize it to make an paradigm source.

Here, I am storing the file name in the MySQL database.

Completed Lawmaking

<?php include("config.php");  if(isset($_POST['but_upload'])){     $name = $_FILES['file']['name'];   $target_dir = "upload/";   $target_file = $target_dir . basename($_FILES["file"]["name"]);    // Select file type   $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));    // Valid file extensions   $extensions_arr = array("jpg","jpeg","png","gif");    // Bank check extension   if( in_array($imageFileType,$extensions_arr) ){      // Upload file      if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name)){         // Insert record         $query = "insert into images(name) values('".$name."')";         mysqli_query($con,$query);      }    }   } ?>  <form method="mail" activity="" enctype='multipart/form-data'>   <input type='file' proper name='file' />   <input type='submit' value='Salvage proper name' name='but_upload'> </form>

Call back

Select the name or path of the image which you accept stored in the database table and utilize information technology in the image source.

Example

<?php  $sql = "select proper noun from images where id=1"; $effect = mysqli_query($con,$sql); $row = mysqli_fetch_array($result);  $image = $row['name']; $image_src = "upload/".$image;  ?> <img src='<?php repeat $image_src;  ?>' >

4. base64_encode()

Yous tin can store the full image in the Database table by converting it into the base64 format. Yous don't demand to shop paradigm reference in the Database table due east.g. name, path, and not require to store the image on your server.

In PHP base64_encode() method is been used for base64 conversion. Before storing it in the database I append data:image/'.$imageFileType.';base64, text with base64 value.

Now when you demand to display the paradigm simply fetch the value and use it equally an image source.

Note – In the case, I upload the image to folder. You can remove the upload lawmaking if yous only want the prototype will attainable through base64 stored values in the database.

Completed Code

<?php include("config.php");  if(isset($_POST['but_upload'])){     $proper noun = $_FILES['file']['proper noun'];   $target_dir = "upload/";   $target_file = $target_dir . basename($_FILES["file"]["name"]);    // Select file blazon   $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));    // Valid file extensions   $extensions_arr = assortment("jpg","jpeg","png","gif");    // Check extension   if( in_array($imageFileType,$extensions_arr) ){     // Upload file     if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name)){        // Catechumen to base64         $image_base64 = base64_encode(file_get_contents('upload/'.$name) );        $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;        // Insert record        $query = "insert into images(image) values('".$image."')";        mysqli_query($con,$query);     }        }   } ?>  <form method="postal service" action="" enctype='multipart/class-information'>   <input blazon='file' name='file' />   <input blazon='submit' value='Salve proper noun' name='but_upload'> </form>

Retrieve

Select the stored base64 value and using it in the prototype source.

Example

<?php   $sql = "select image from images order by id desc limit 1";  $issue = mysqli_query($con,$sql);  $row = mysqli_fetch_array($result);   $image_src = $row['image'];   ?> <img src='<?php echo $image_src; ?>' >

five. Conclusion

In my stance, instead of storing an prototype in the MySQL database in the base64 format, information technology'due south better to store it in the server and salve the reference in the database tabular array to keep track of the location.

Information technology is fast and consumes less infinite in the database table compare to base64.

You can view this tutorial to know how you tin can store a video file in the MySQL database.

If you constitute this tutorial helpful then don't forget to share.

Are you want to get implementation assist, or change or extend the functionality of this script? Submit paid service request.

Related posts:

borgerbeell1944.blogspot.com

Source: https://makitweb.com/upload-and-store-an-image-in-the-database-with-php/

Post a Comment for "Php Display Preview Image Upload to Database"