banner



How To Upload Image Php7

You can salve your uploading images in the database table for afterward employ e.g. display user profile or product image, create the epitome gallery, etc.

In that location are two means of doing this –

  • Salve the path or proper noun of an prototype
  • Encode image into a base64 format

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

Upload and store an image in the Database with PHP


Contents

  1. Table structure
  2. Configuration
  3. Save path or proper noun
  4. base64_encode()
  5. Determination

1. Tabular array structure

In the case, I am using images table for storing information.

  • proper name – This field is used to store the epitome file name.
  • image – This field is used to shop the epitome base64 generated value.
CREATE TABLE `images` (   `id` int(11) Not NULL Primary KEY AUTO_INCREMENT,   `name` varchar(200) Non NULL,   `image` longtext Non Cipher ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a new config.php file for database configuration.

Completed Lawmaking

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

3. Save path or proper name

You lot can either save the full path or name of an image in your MySQL database table. Retrieve the image name or path from the MySQL database and apply 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");    // 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(proper noun) values('".$name."')";         mysqli_query($con,$query);      }    }   } ?>  <form method="postal service" action="" enctype='multipart/form-data'>   <input type='file' proper name='file' />   <input type='submit' value='Relieve proper name' name='but_upload'> </course>

Retrieve

Select the name or path of the epitome which you accept stored in the database table and use it in the image source.

Example

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

4. base64_encode()

You can store the full epitome in the Database table by converting it into the base64 format. You lot don't need to store epitome reference in the Database table e.g. proper noun, path, and non 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 suspend data:image/'.$imageFileType.';base64, text with base64 value.

Now when you demand to display the image just fetch the value and utilise it every bit an image source.

Notation – In the example, I upload the image to folder. You can remove the upload code if you only want the prototype volition attainable through base64 stored values in the 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 = assortment("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) );        $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;        // Insert tape        $query = "insert into images(prototype) values('".$epitome."')";        mysqli_query($con,$query);     }        }   } ?>  <form method="post" activity="" enctype='multipart/form-data'>   <input blazon='file' name='file' />   <input blazon='submit' value='Salve name' proper noun='but_upload'> </form>

Retrieve

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

Example

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

5. Conclusion

In my opinion, instead of storing an image in the MySQL database in the base64 format, it'south better to shop it in the server and salve 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 tin view this tutorial to know how you can shop a video file in the MySQL database.

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

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

Related posts:

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

Posted by: deleonineaskuld.blogspot.com

0 Response to "How To Upload Image Php7"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel