Php Upload Image With Caption and Display

You can save your uploading images in the database table for later utilise due east.g. display user contour or product image, create the epitome gallery, etc.

There are two ways of doing this –

  • Save the path or proper name of an image
  • Encode paradigm into a base64 format

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

Upload and store an image in the Database with PHP


Contents

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

1. Table structure

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

  • name – This field is used to shop the paradigm file name.
  • image – This field is used to store the prototype base64 generated value.
CREATE Tabular array `images` (   `id` int(11) Non NULL Chief Fundamental AUTO_INCREMENT,   `name` varchar(200) Not Zip,   `epitome` longtext NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a new config.php file for database configuration.

Completed Code

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

3. Save path or name

You can either save the full path or proper noun of an image in your MySQL database table. Retrieve the epitome proper noun or path from the MySQL database and use it to make an prototype source.

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

Completed Code

<?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");    // Check extension   if( in_array($imageFileType,$extensions_arr) ){      // Upload file      if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name)){         // Insert tape         $query = "insert into images(name) values('".$name."')";         mysqli_query($con,$query);      }    }   } ?>  <course method="post" action="" enctype='multipart/form-data'>   <input type='file' proper noun='file' />   <input type='submit' value='Save name' name='but_upload'> </form>

Retrieve

Select the name or path of the image which yous have stored in the database table and utilize it in the image source.

Example

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

four. base64_encode()

You can store the full image in the Database table by converting it into the base64 format. You don't need to store image reference in the Database table eastward.g. proper noun, path, and not crave to shop the image on your server.

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

At present when you need to brandish the image just fetch the value and use it equally an image source.

Annotation – In the case, I upload the paradigm to binder. You lot can remove the upload code if you only desire the image will accessible through base64 stored values in the database.

Completed Code

<?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)){        // Convert to base64         $image_base64 = base64_encode(file_get_contents('upload/'.$name) );        $epitome = 'information:image/'.$imageFileType.';base64,'.$image_base64;        // Insert record        $query = "insert into images(image) values('".$image."')";        mysqli_query($con,$query);     }        }   } ?>  <form method="mail" action="" enctype='multipart/form-data'>   <input type='file' name='file' />   <input type='submit' value='Save proper noun' name='but_upload'> </form>

Retrieve

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

Case

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

5. Conclusion

In my opinion, instead of storing an epitome in the MySQL database in the base64 format, it's better to shop it in the server and save the reference in the database table to keep track of the location.

It is fast and consumes less space in the database table compare to base64.

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

If y'all establish this tutorial helpful then don't forget to share.

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

Related posts:

harmshimmuch57.blogspot.com

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

0 Response to "Php Upload Image With Caption and Display"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel