Upload Image Upload Image and Get Url
You can salve your uploading images in the database table for later use e.k. brandish user profile or product image, create the image gallery, etc.
At that place are 2 ways of doing this –
- Save the path or proper noun of an epitome
- Encode epitome into a base64 format
In this tutorial, I show you both of the methods for storing and retrieving an image from the database tabular array.
Contents
- Table construction
- Configuration
- Salvage path or name
- base64_encode()
- Conclusion
1. Tabular array structure
In the example, I am using images table for storing data.
- name – This field is used to store the prototype file name.
- prototype – This field is used to shop the prototype base64 generated value.
CREATE Table `images` ( `id` int(11) NOT Zippo PRIMARY Key AUTO_INCREMENT, `proper noun` varchar(200) Non Nada, `image` longtext NOT Nil ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
two. Configuration
Create a new config.php file for database configuration.
Completed Lawmaking
<?php $host = "localhost"; /* Host proper noun */ $user = "root"; /* User */ $password = ""; /* Countersign */ $dbname = "tutorial"; /* Database name */ $con = mysqli_connect($host, $user, $password,$dbname); // Check connection if (!$con) { die("Connection failed: " . mysqli_connect_error()); } 3. Salvage path or proper noun
You lot can either relieve the full path or proper name of an image in your MySQL database tabular array. Retrieve the prototype proper noun or path from the MySQL database and employ information technology 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 record $query = "insert into images(proper noun) values('".$name."')"; mysqli_query($con,$query); } } } ?> <class method="post" activity="" enctype='multipart/class-data'> <input blazon='file' proper noun='file' /> <input type='submit' value='Save name' name='but_upload'> </form> Retrieve
Select the name or path of the image which you accept stored in the database table and use it in the image source.
Example
<?php $sql = "select name from images where id=1"; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); $image = $row['name']; $image_src = "upload/".$image; ?> <img src='<?php echo $image_src; ?>' >
4. base64_encode()
You tin store the total image in the Database table by converting it into the base64 format. You lot don't demand to store image reference in the Database tabular array e.m. name, path, and not require to store the epitome 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 use it equally an image source.
Note – In the case, I upload the image to folder. You can remove the upload code if you simply want the prototype will attainable 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) ); $prototype = 'data:prototype/'.$imageFileType.';base64,'.$image_base64; // Insert record $query = "insert into images(epitome) values('".$image."')"; mysqli_query($con,$query); } } } ?> <form method="post" action="" enctype='multipart/form-data'> <input blazon='file' name='file' /> <input blazon='submit' value='Salvage proper noun' name='but_upload'> </course> 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"; $effect = mysqli_query($con,$sql); $row = mysqli_fetch_array($effect); $image_src = $row['epitome']; ?> <img src='<?php echo $image_src; ?>' >
5. Decision
In my opinion, instead of storing an image in the MySQL database in the base64 format, it's ameliorate to store information technology in the server and save the reference in the database table to keep rail of the location.
It is fast and consumes less space in the database tabular array compare to base64.
You can view this tutorial to know how you can shop a video file in the MySQL database.
If you found this tutorial helpful then don't forget to share.
Are you lot want to become implementation aid, or alter 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/
Post a Comment for "Upload Image Upload Image and Get Url"