如何使用 file_get_contents() 读取上传的图片?

问题描述

我在 MysqL 中有一个 BLOB 列,想要插入上传的图像。通过阅读一些reference,我尝试使用file_get_contents($path)插入图像;但经过反复试验,BLOB 列保持 NULL 而 mime 列正确更新。可能是由于某些文件读取权限吗?那个 $image = file_get_contents($_FILES["profilePicture"]["tmp_name"]); 行有问题吗?

上传表单

<form method="post" action="index.PHP?reqPage=profile" enctype="multipart/form-data">
<div class="row mb-3">
    <label for="profilePicture" class="form-label">Upload a photo for your profile</label>
    <input class="form-control" type="file" name="profilePicture" accept="image/jpg,image/png,image/gif,image/jpeg">
</div>
<div class="row justify-content-center mb-3">
    <button type="submit" class="btn btn-primary">Save</button>
</div>
</form>

profile.PHP

if ( isset($_FILES["profilePicture"]) && $_FILES["profilePicture"]["error"] == 0) { //if photo is uploaded without error
  $type = $_FILES["profilePicture"]["type"]; //browser provided mime
  $size = $_FILES["profilePicture"]["size"]; //size in bytes
  $filePath = $_FILES["profilePicture"]["tmp_name"]; //temporary path
  $image = file_get_contents($filePath);
  
  $sql = "UPDATE profiles SET profilePicture = ?,profilePictureMIME = ? WHERE 
  user = ?"; //for each user in profiles,profilePicture & MIME were initially null
  $param = [$image,$type,$user];
  $stmt = $dbh->prepare($sql);
  $stmt->execute($param);
}

配置文件表中的列

user VARCHAR(20),profilePicture BLOB,profilePictureMIME VARCHAR(30),

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)