How to Upload Image Store in Local Path
Yous can salve your uploading images in the database table for later use e.1000. display user contour or production image, create the image gallery, etc.
At that place are two ways of doing this –
- Save the path or name of an prototype
- Encode paradigm into a base64 format
In this tutorial, I show y'all both of the methods for storing and retrieving an image from the database table.
Contents
- Table structure
- Configuration
- Save path or proper noun
- base64_encode()
- Decision
1. Table structure
In the example, I am using images tabular array for storing data.
- proper noun – This field is used to store the image file name.
- image – This field is used to store the image base64 generated value.
CREATE TABLE `images` ( `id` int(11) NOT NULL PRIMARY Cardinal AUTO_INCREMENT, `name` varchar(200) NOT Cipher, `paradigm` 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 = ""; /* Password */ $dbname = "tutorial"; /* Database name */ $con = mysqli_connect($host, $user, $password,$dbname); // Cheque connection if (!$con) { dice("Connection failed: " . mysqli_connect_error()); } 3. Save path or name
You lot can either salve the full path or name of an image in your MySQL database tabular array. Call up the image name or path from the MySQL database and use information technology to make an epitome source.
Hither, 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"]["proper noun"]); // Select file type $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Valid file extensions $extensions_arr = array("jpg","jpeg","png","gif"); // Cheque extension if( in_array($imageFileType,$extensions_arr) ){ // Upload file if(move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$proper noun)){ // Insert record $query = "insert into images(proper noun) values('".$name."')"; mysqli_query($con,$query); } } } ?> <form method="post" action="" enctype='multipart/form-information'> <input type='file' name='file' /> <input blazon='submit' value='Save proper noun' name='but_upload'> </form> Recollect
Select the proper noun or path of the image which you lot take stored in the database table and employ information technology in the image source.
Example
<?php $sql = "select name from images where id=1"; $issue = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); $image = $row['name']; $image_src = "upload/".$prototype; ?> <img src='<?php repeat $image_src; ?>' >
four. base64_encode()
You can store the full image in the Database table past converting it into the base64 format. You don't need to shop epitome reference in the Database tabular array e.g. name, path, and not require to store the image on your server.
In PHP base64_encode() method is been used for base64 conversion. Earlier storing information technology in the database I append data:prototype/'.$imageFileType.';base64, text with base64 value.
At present when you lot need to display the image just fetch the value and use it every bit an prototype source.
Note – In the example, I upload the epitome to folder. You can remove the upload code if you only want the epitome will 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"]["name"]); // Select file type $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Valid file extensions $extensions_arr = array("jpg","jpeg","png","gif"); // Cheque 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/'.$proper noun) ); $image = 'information:epitome/'.$imageFileType.';base64,'.$image_base64; // Insert record $query = "insert into images(image) values('".$image."')"; mysqli_query($con,$query); } } } ?> <form method="post" action="" enctype='multipart/form-information'> <input blazon='file' name='file' /> <input blazon='submit' value='Save name' name='but_upload'> </course> Recollect
Select the stored base64 value and using information technology in the image source.
Example
<?php $sql = "select paradigm from images order by id desc limit ane"; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); $image_src = $row['paradigm']; ?> <img src='<?php echo $image_src; ?>' >
5. Conclusion
In my opinion, instead of storing an paradigm in the MySQL database in the base64 format, it's improve to store information technology in the server and save the reference in the database table to continue track of the location.
Information technology is fast and consumes less infinite in the database table compare to base64.
You can view this tutorial to know how you tin can store a video file in the MySQL database.
If you found this tutorial helpful then don't forget to share.
Are yous desire to become 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/
0 Response to "How to Upload Image Store in Local Path"
Post a Comment