Uploading Images to an Images Directory Php

You can salve your uploading images in the database table for later use east.g. display user contour or production image, create the image gallery, etc.

There are ii ways of doing this –

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

In this tutorial, I testify y'all 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. Table structure
  2. Configuration
  3. Save path or proper name
  4. base64_encode()
  5. Conclusion

i. Table construction

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

  • proper name – This field is used to shop the image file proper noun.
  • image – This field is used to store the paradigm base64 generated value.
CREATE TABLE `images` (   `id` int(11) Non NULL PRIMARY KEY AUTO_INCREMENT,   `name` varchar(200) NOT NULL,   `image` 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 name */ $user = "root"; /* User */ $password = ""; /* Countersign */ $dbname = "tutorial"; /* Database name */  $con = mysqli_connect($host, $user, $password,$dbname); // Check connection if (!$con) {   dice("Connectedness failed: " . mysqli_connect_error()); }

3. Salve path or proper name

You tin either salvage the full path or proper name of an image in your MySQL database table. Recall the epitome name or path from the MySQL database and utilise it to make an image source.

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

Completed Code

<?php include("config.php");  if(isset($_POST['but_upload'])){     $proper noun = $_FILES['file']['name'];   $target_dir = "upload/";   $target_file = $target_dir . basename($_FILES["file"]["proper noun"]);    // Select file blazon   $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/grade-data'>   <input blazon='file' name='file' />   <input type='submit' value='Save name' proper name='but_upload'> </grade>

Retrieve

Select the proper noun or path of the image which you accept stored in the database table and utilize it in the paradigm source.

Instance

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

4. base64_encode()

You can shop the total image in the Database tabular array past converting it into the base64 format. You don't need to store image reference in the Database table e.g. proper name, path, and not require 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:prototype/'.$imageFileType.';base64, text with base64 value.

Now when yous need to display the image just fetch the value and use it as an image source.

Annotation – In the example, I upload the prototype to folder. Y'all tin can remove the upload lawmaking if yous just want the paradigm will accessible through base64 stored values in the database.

Completed Code

<?php include("config.php");  if(isset($_POST['but_upload'])){     $proper 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");    // Check extension   if( in_array($imageFileType,$extensions_arr) ){     // Upload file     if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$proper name)){        // Convert to base64         $image_base64 = base64_encode(file_get_contents('upload/'.$proper noun) );        $paradigm = 'data:paradigm/'.$imageFileType.';base64,'.$image_base64;        // Insert record        $query = "insert into images(epitome) values('".$epitome."')";        mysqli_query($con,$query);     }        }   } ?>  <class method="post" activity="" enctype='multipart/form-data'>   <input type='file' name='file' />   <input type='submit' value='Save name' name='but_upload'> </form>

Retrieve

Select the stored base64 value and using information technology in the epitome source.

Case

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

five. Conclusion

In my opinion, instead of storing an image in the MySQL database in the base64 format, it'southward improve to shop information technology in the server and relieve the reference in the database table to continue rails 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 y'all tin store a video file in the MySQL database.

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

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

Related posts:

skidmorefard1990.blogspot.com

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

0 Response to "Uploading Images to an Images Directory Php"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel