Upload Image Profile Into Your Webpage Php
You can save your uploading images in the database tabular array for later use e.chiliad. display user profile or product image, create the image gallery, etc.
There are two means of doing this –
- Save the path or name of an image
- Encode epitome into a base64 format
In this tutorial, I evidence you both of the methods for storing and retrieving an image from the database table.
Contents
- Table structure
- Configuration
- Save path or name
- base64_encode()
- Conclusion
1. Tabular array structure
In the instance, I am using images
table for storing data.
- name – This field is used to store the image file name.
- image – This field is used to store the image base64 generated value.
CREATE Tabular array `images` ( `id` int(11) NOT NULL Principal KEY AUTO_INCREMENT, `proper name` varchar(200) Non Nix, `image` longtext Non NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
two. 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 noun */ $con = mysqli_connect($host, $user, $password,$dbname); // Check connexion if (!$con) { die("Connection failed: " . mysqli_connect_error()); }
3. Relieve path or proper name
Yous can either relieve the full path or proper noun of an image in your MySQL database tabular array. Retrieve the image proper noun or path from the MySQL database and utilise it to make an epitome source.
Hither, I am storing the file name in the MySQL database.
Completed Lawmaking
<?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 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 record $query = "insert into images(name) values('".$proper name."')"; mysqli_query($con,$query); } } } ?> <form method="postal service" action="" enctype='multipart/form-data'> <input type='file' name='file' /> <input type='submit' value='Salve name' name='but_upload'> </form>
Retrieve
Select the name or path of the image which y'all have stored in the database tabular array and use information technology in the image source.
Example
<?php $sql = "select name from images where id=i"; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($event); $image = $row['proper name']; $image_src = "upload/".$image; ?> <img src='<?php repeat $image_src; ?>' >
iv. base64_encode()
You can store the full prototype in the Database tabular array by converting it into the base64 format. You don't need to store epitome reference in the Database table e.g. name, path, and non require to store the image on your server.
In PHP base64_encode()
method is been used for base64 conversion. Earlier storing it in the database I append information:prototype/'.$imageFileType.';base64,
text with base64 value.
Now when yous demand to brandish the prototype but fetch the value and use it as an image source.
Annotation – In the example, I upload the image to folder. Y'all can remove the upload code if y'all only want the image volition accessible 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"]["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.$proper noun)){ // Convert to base64 $image_base64 = base64_encode(file_get_contents('upload/'.$name) ); $image = 'information:epitome/'.$imageFileType.';base64,'.$image_base64; // Insert record $query = "insert into images(image) values('".$image."')"; mysqli_query($con,$query); } } } ?> <form method="post" activeness="" enctype='multipart/form-information'> <input blazon='file' name='file' /> <input type='submit' value='Save proper noun' name='but_upload'> </class>
Retrieve
Select the stored base64 value and using it in the image source.
Case
<?php $sql = "select epitome from images order by id desc limit ane"; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); $image_src = $row['image']; ?> <img src='<?php repeat $image_src; ?>' >
5. Conclusion
In my opinion, instead of storing an image in the MySQL database in the base64 format, it'south better to store it in the server and relieve the reference in the database table to keep track of the location.
It is fast and consumes less infinite in the database tabular array compare to base64.
Yous can view this tutorial to know how you can store a video file in the MySQL database.
If you found this tutorial helpful then don't forget to share.
Are you want to go implementation assist, or modify or extend the functionality of this script? Submit paid service asking.
Related posts:
Source: https://makitweb.com/upload-and-store-an-image-in-the-database-with-php/
0 Response to "Upload Image Profile Into Your Webpage Php"
Post a Comment