简体   繁体   中英

Display img from database with php not working

I managed to upload images into a database(lonblob). I have issues display that image:

Upload:

if(isset($_POST['pic_upload'])){
    if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
        echo "Please select an image.";
    }else{
        $image= addslashes($_FILES['image']['tmp_name']);
        $name= addslashes($_FILES['image']['name']);
        $image= file_get_contents($image);
        $image= base64_encode($image);
        saveimage($name,$image);
    }
}

saveimage(writes the $image into a db field(type longblob))

Get images:

function get_image($userid){
    $sql = "SELECT * FROM images WHERE user_id = ".$userid."";
    $result = execute_sql($sql);
    return $result;
}

display image:

<?php 
    $row = mysqli_fetch_array($img);
    echo '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode($row[3]).'">'; 
?>

The upload works perfectly, but the image doesn't appear on the site.

You're encoding it twice.

<?php 
    $row = mysqli_fetch_array($img);
    echo '<img height="300" width="300" src="data:image/jpeg;base64,'.$row[3].'">'; 
?>

Also, I would recommend looking at pathinfo() , finding the extension a file on upload?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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