PHP display image BLOB from MySQL
Try it like this.
For inserting into DB
$db = new mysqli("localhost", "root", "", "DbName");
$image = file_get_contents($_FILES['images']['tmp_name']);
$query = "INSERT INTO products (image) VALUES(?)";
$stmt = $db->prepare($query);
$stmt->bind_param('s', $image);
$stmt->execute();
For accessing image from Blob
$db = new mysqli("localhost", "root", "", "DbName");
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
Display blob image from database in php
You can try it using a base64
encoded string as the source for the image, e.g.:
echo '<div class="caption"><h3><img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>'. $row['NAME']. '</h3></div>';
PHP : How to display image from blob
The src
attribute of an image MUST point at a url. You cannot dump the raw binary contents of an image in there and expect it to work. The browser will take that raw binary data and try to hit the page's originating server and request that data as if it was a file url. i.e. you have this on a page loaded from http://example.com/foo/bar/baz.php
:
<img src="blahblahblahblah" />
which will result in the browser requesting
http://example.com/foo/bar/blahblahblahblah
If you want to embed your image in the page, then you have to use a Data URI:
<img src="data:image/jpeg;base64,<?php echo $base64_encoded_image ?>" />
How to Display a BLOB Image from database?
You forgot to take the right raw data. Note this line:
$blobimage = addslashes(file_get_contents($_FILES['files']['tmp_name'][$key]));
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$blobimage = addslashes(file_get_contents($_FILES['files']['tmp_name'][$key]));
$filename = $_FILES['files']['name'][$key];
$path = $_FILES['files']['name'][$key];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$desired_dir="upload/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir".$filename.'.'.$ext)==false){
move_uploaded_file($_FILES['files']['tmp_name'][$key], $desired_dir.$filename.'.'.$ext);
}else{
$new_dir=$desired_dir.$filename.'.'.$ext.time();
rename($desired_dir.$filename.'.'.$ext,$new_dir) ;
}
}else{
print_r($errors);
}
if(empty($error)){
$query="INSERT INTO tbl_raw_image (image) VALUES('$blobimage'); ";
$rs = mysqli_query($conn, $query);
}
}
Related Topics
Target Class Controller Does Not Exist - Laravel 8
Escaping Quotation Marks in PHP
How to Decode Unicode Escape Sequences Like "\U00Ed" to Proper Utf-8 Encoded Characters
File_Get_Contents(): Ssl Operation Failed With Code 1, Failed to Enable Crypto
How to Add Elements to an Empty Array in PHP
How to Validate an Email in PHP
Laravel Requires the Mcrypt PHP Extension
How to Escape Strings in SQL Server Using PHP
Simple Xml - Dealing With Colons in Nodes
How to Force File Download With PHP
How to Store My Users' Passwords Safely
How to "Flatten" a Multi-Dimensional Array to Simple One in PHP
How to Convert Xml into Array in PHP
How to Create a Secure MySQL Prepared Statement in PHP
Force Ssl/Https Using .Htaccess and Mod_Rewrite
Best Way to Get Result Count Before Limit Was Applied
Https and Ssl3_Get_Server_Certificate:Certificate Verify Failed, Ca Is Ok