php - how to display blob images -
i trying insert & display multiple blob images because of mistake not appearing page , multiple images not getting saved, code follows: test1.php[inserting blob images]:
$conn = mysqli_connect("localhost", "root", "root", "test"); for($i=0; $i<count($_files["userfile"]["name"]); $i++){ $imagename = mysqli_real_escape_string($conn, $_files["userfile"]["name"]); $imagedata = mysqli_real_escape_string($conn, $_files["userfile"]["tmp_name"]); $imagetype = mysqli_real_escape_string($conn, $_files["userfile"]["type"]); $image = file_get_contents($imagedata); $image = base64_encode($image); if($imagename!=''){ mysqli_query($conn, "insert files set mime = '".$imagename."', data= '".$image."' "); $result = array("insert_res"=>"110"); echo json_encode($result); }else{ $result = array("insert_res"=>"111"); echo json_encode($result); } } ?> <!doctype html> <html> <head> <title>image upload</title> </head> <body> <!-- <form name="" action="<?php echo $_server["php_self"]; ?>" id="" method="post" enctype="multipart/form-data"> <input type="file" name="image" id="image" /> <input type="submit" name="submit" value="upload" />--> <img src="test_dis.php?id=1" /> <!--</form>--> </body> </html>
test_disp.php[display images database]:
$conn = mysqli_connect("localhost", "root", "root", "test"); if(isset($_get['id'])){ $id = mysqli_real_escape_string($conn, $_get['id']); $sql = "select * files id = '".$id."' "; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_assoc($result)){ $imagedata = $row[data]; } header("contetnt-type: image/jpg"); echo $imagedata; } else{ echo 'error'; }
for single image working fine, when there multilple images not inserting , not fetching. m getting these images ios app. how solve ?please hlp while displaying coming this.
after insertion repeating first image 3 times ios app getting 3 images only, skipping 1 image
first not looping on $files
array properly. need add $i
loop counter $files....
references
$conn = mysqli_connect("localhost", "root", "root", "test"); for($i=0; $i<count($_files["userfile"]["name"]); $i++){ $imagename = mysqli_real_escape_string($conn, $_files["userfile"]["name"][$i]); $imagedata = mysqli_real_escape_string($conn, $_files["userfile"]["tmp_name"][$i]); $imagetype = mysqli_real_escape_string($conn, $_files["userfile"]["type"][$i]); $image = file_get_contents($imagedata); $image = base64_encode($image); if($imagename!=''){ $res = mysqli_query($conn, "insert files set mime = '$imagename', data= '$image'"); if ( $res === false ) { // check insert worked, reply error 112 if not $e = mysqli_error($conn); echo json_encode( array( 'insert_res'=>'112' 'insert_error' => $e ) ); } else { echo json_encode(array("insert_res"=>"110")); } }else{ $result = array("insert_res"=>"111"); echo json_encode($result); } } ?>
you have encoded original image i.e. base64_encode()
need reverse encoding becomes image again.
test_disp.php
$conn = mysqli_connect("localhost", "root", "root", "test"); if(isset($_get['id'])){ $id = mysqli_real_escape_string($conn, $_get['id']); $sql = "select * files id = '$id'"; $result = mysqli_query($conn, $sql); if ( $result === false ) { // check query worked echo mysqli_error($conn); exit; } $row = mysqli_fetch_assoc($result); header("content-type: image/jpg"); echo base64_decode($row[data]); // < added function } else { echo 'error'; }
you dont need while loop when query return 1 result row!
Comments
Post a Comment