Display all images stored as BLOB data from MySQL database
First you need to remove the if(isset($_GET['id'])
statement because you want to display all images.
Next, query all images by changing your query to query without an id
$query = mysql_query("select * from `blob`");
Next, store the image data to an array.
$images = array();
while ($row = mysql_fetch_assoc($query)) {
$images[] = $row['image'];
}
Finally, display all images. (Got the code to display multiple blobs from here)
/* header should be removed
header("content-type: image/jpeg"); */
foreach ($images as $image) {
echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}
Displaying BLOB image from Mysql database into dynamic div in html
1) Base64 option
Work with a single line, image/png
for a png image and image/jpeg
for a jpg one :
echo '<img src="data:image/png;base64,'.base64_encode($blob).'"/>';
example :
<div style="background-color:black; text-align:center; padding: 5px;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAwBAMAAACh2TSJAAAALVBMVEUAAADtNTX////3n5/+9fX719f7zMz5tLTzfHzuQED//f31jY3ybGzxXV3wVFRaxp+rAAAAAXRSTlMAQObYZgAAALVJREFUOMut0rENAjEQRNHdC4kY0QBaAQUQX0QAFSAKIKQEKiAA6VqgIkriApuV1x7pQPz0aWwHljLMpZ0CRDBGoXmeghGYKFJsUo90giAImCgV5OJF+oOgKE48MlGgs2VLBIunWesw0a1ZHqF82c7GmmIfUSpgotOly29DFPFJFDEhkgIT/V5mZuvj6XofKrHU6vyI4u37IYi36aN4h5tL7PJyif1dvCgEpapzISbCTEj5R78BZq5A5Ldh2XYAAAAASUVORK5CYII"></div>
How to display an BLOB image stored in MySql database?
This is what I used when I wanted to do something like that... a long time ago! =P
$sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
$result = mysqli_query($db,$sql);
while($arraySomething = mysqli_fetch_array($result))
{
echo "<img src='php/imgView.php?imgId=".$arraySomething."' />";
}
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']).'"/>';
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
PHP Datetime() Class, Change First Day of the Week to Monday
Special Characters in "File_Exists" Problem (Php)
How to Pass Custom Type Array to Postgres Function
Getting the Name of a Child Class in the Parent Class (Static Context)
Can Servers Block Curl Requests
Run a PHP Script Every Second Using Cli
PHP Pass Function Name as Param Then Call the Function
Explode() into $Key=>$Value Pair
Protection Against Xss Exploits
PHP String Manipulation: Extract Hrefs
Running a Zend Framework Action from Command Line
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid
Codeigniter: Try Catch Is Not Working in Model Class