Hamza
Hamza

Reputation: 6025

How to Display a BLOB Image from database?

I know there are several questions on the same topic however none of them actually resolved my issue. I have tried approaches from the following questions:

But every approach leads to the same result. I'm getting this sort of thing instead of the image: enter image description here My code for database connection is same for insertion and I can successfully insert images its just they are not getting displayed. Still I'm pasting my codes in case they're relevant:

Connection String:

	  $user = "root";
    $pass = "";
    $db = "raw_images";


	$conn = mysqli_connect('localhost',$user,$pass,$db);
	if(!$conn)
	{
		die(mysqli_error());
	}

Display code:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

Insert Code:

<!--HTML Form-->

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple class="Box"/>
    <input type="submit" class="Default"/>
</form>

<!--PHP Code-->

$imageData = array();
if(isset($_FILES['files'])){
    $errors= array();
	foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
		$file_name = $key.$_FILES['files']['name'][$key];
		$file_tmp =$_FILES['files']['tmp_name'][$key];

        array_push($imageData, $file_name);

        $desired_dir="user_data";
        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/".$file_name)==false){
                move_uploaded_file($file_tmp,"user_data/".$file_name);
            }else{									//rename the file if another one exist
                $new_dir="user_data/".$file_name.time();
                 rename($file_tmp,$new_dir) ;
            }

        }else{
                print_r($errors);
        }

	if(empty($error)){

		$imgDt = implode("|", $imageData);
		$query="INSERT INTO tbl_raw_image (image) VALUES('$imgDt'); ";
		$rs  = mysqli_query($conn, $query);
    $imageData = array();
	}
}

}

Upvotes: 0

Views: 3261

Answers (1)

Mawia HL
Mawia HL

Reputation: 3665

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);
    }
}

Upvotes: 1

Related Questions