简体   繁体   English

如何从数据库显示BLOB图像?

[英]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: 我得到这种东西而不是图像: 在此处输入图片说明 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(); } } } 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM