Php Create a Web Form for Uploading Pictures

You can salvage your uploading images in the database tabular array for after apply e.1000. brandish user profile or production image, create the image gallery, etc.

At that place are 2 ways of doing this –

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

In this tutorial, I testify you both of the methods for storing and retrieving an paradigm from the database table.

Upload and store an image in the Database with PHP


Contents

  1. Table construction
  2. Configuration
  3. Save path or proper name
  4. base64_encode()
  5. Conclusion

1. Tabular array structure

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

  • name – This field is used to shop the image file name.
  • paradigm – This field is used to shop the image base64 generated value.
CREATE Tabular array `images` (   `id` int(11) NOT Goose egg Chief KEY AUTO_INCREMENT,   `name` varchar(200) NOT NULL,   `image` longtext Non 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 = ""; /* Password */ $dbname = "tutorial"; /* Database proper name */  $con = mysqli_connect($host, $user, $countersign,$dbname); // Bank check connexion if (!$con) {   die("Connection failed: " . mysqli_connect_error()); }

iii. Relieve path or name

Yous 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 employ 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'])){     $name = $_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 = 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.$proper name)){         // Insert record         $query = "insert into images(name) values('".$proper name."')";         mysqli_query($con,$query);      }    }   } ?>  <form method="post" action="" enctype='multipart/form-information'>   <input type='file' name='file' />   <input type='submit' value='Save name' name='but_upload'> </class>

Retrieve

Select the name or path of the image which y'all have stored in the database table and use information technology in the paradigm source.

Case

<?php  $sql = "select proper noun from images where id=ane"; $event = mysqli_query($con,$sql); $row = mysqli_fetch_array($upshot);  $paradigm = $row['name']; $image_src = "upload/".$epitome;  ?> <img src='<?php echo $image_src;  ?>' >

4. base64_encode()

You tin can store the full prototype in the Database table by converting information technology into the base64 format. Y'all don't demand to store image reference in the Database table e.g. name, path, and not crave to store the prototype on your server.

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

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

Note – In the instance, I upload the image to folder. Y'all can remove the upload code if you only want the epitome volition accessible through base64 stored values in the database.

Completed Lawmaking

<?php include("config.php");  if(isset($_POST['but_upload'])){     $name = $_FILES['file']['proper 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.$name)){        // Convert to base64         $image_base64 = base64_encode(file_get_contents('upload/'.$name) );        $image = 'data:prototype/'.$imageFileType.';base64,'.$image_base64;        // Insert record        $query = "insert into images(paradigm) values('".$image."')";        mysqli_query($con,$query);     }        }   } ?>  <class method="mail service" action="" enctype='multipart/form-data'>   <input type='file' name='file' />   <input blazon='submit' value='Save name' name='but_upload'> </form>

Retrieve

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

Example

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

5. Conclusion

In my stance, instead of storing an image in the MySQL database in the base64 format, information technology's better to shop it in the server and save the reference in the database table to keep runway of the location.

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

Yous can view this tutorial to know how you lot can shop a video file in the MySQL database.

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

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

Related posts:

robertsconve2002.blogspot.com

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

0 Response to "Php Create a Web Form for Uploading Pictures"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel