-1

Snippet:

$connect = //connect to db;
$db = $connect->prepare("SELECT profilepicture FROM user WHERE uname = $uname");
$db->execute();
$db->bind_result($img);

...

echo $img;

Information:
- I have image.bin stored in a column of a table in db
- Opening image.bin in hex editor reveals hex codes (File header says it is a PNG file)
- echo $img doesn't output the image

Question: - How to output images that are stored in db?

Timothy Wong
  • 689
  • 3
  • 9
  • 28

1 Answers1

0

Form

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

This is how you upload the file

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

$img = basename($_FILES["fileToUpload"]["name"]);

Your insert query
$db = $connect->prepare("UPDATE user SET profilepicture =? WHERE uname = ?");
$db->bind_param("si",$img,$uname);
$db->execute();

Taken from w3schools

Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31