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

des4xlb0  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(195)

我在mysql中有一个blob列,希望插入一个上传的图像。通过阅读一些参考资料,我尝试使用 file_get_contents($path) ; 但经过反复试验,blob列仍然为空,而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),

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题