使用php输出存储在数据库MySQL中的图像

时间:2017-01-23 12:24:29

标签: php mysql

段:

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

...

echo $img;

信息:
- 我将image.bin存储在db
中的表的列中 - 在十六进制编辑器中打开image.bin显示十六进制代码(文件标题表示它是PNG文件)
- echo $img无法输出图片

问题: - 如何输出存储在db?

中的图像

1 个答案:

答案 0 :(得分:0)

表格

<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>

这是您上传文件的方式

$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();

取自w3schools