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

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

我在mysql中有一个blob列,希望插入一个上传的图像。通过阅读一些参考资料,我尝试使用 file_get_contents($path) ; 但经过反复试验,blob列仍然为空,而mime列则正确更新。这可能是由于某些文件读取权限造成的吗?是吗 $image = file_get_contents($_FILES["profilePicture"]["tmp_name"]); 线路有问题吗?
上传表单

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

profile.php

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

配置文件表中的列

  1. user VARCHAR(20),
  2. profilePicture BLOB,
  3. profilePictureMIME VARCHAR(30),

暂无答案!

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

相关问题