简体   繁体   中英

php mysql upload an image to database

Found several examples online on how to upload an image to a mysql database table, in binary NOT a link or a folder in the server.

the imag isn't viewable when i try to print it

when I check the database table,it shows a bunch of data in weird format, i'm assuming the image data

here is code

if(!empty($_FILES['image']) && $_FILES['image']['size'] > 0 && !empty($_POST['name']))
{
    // Temporary file name stored on the server
       $tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

code for displaying image

$sql = "SELECT * FROM `photos` WHERE userName = '$currentUser'";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)) 
    {
        $content = $row['image'];
        echo $content;
        echo '<p id="caption">'.$row['caption'].' </p>';
    }

when i try to display the image i get nothing as output, i check the database via putty, and i see a massive amount of weird characters, i'm assuming the items for the image.

ideas?

you could eventually try to replace these two lines:
$content = $row['image']; echo $content;
with:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" />';

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