我正在尝试将图片与路径文件一起保存到我的数据库中。但它现在的做法是不正确的。它仅将图像保存到数据库中,而不是整个图像路径。怎么了?我在另一个项目中使用此代码做了完全相同的事情,但无法解决这里的问题。$userPic = '';$date_time = date('Y-m-d_H-i-s');if(!empty($userLoggedIn)) { if (isset($_FILES['fileToUpload'])) { $errors = array(); $file_name = $_FILES['fileToUpload']['name']; $file_size = $_FILES['fileToUpload']['size']; $width = 1500; $height = 1500; $file_tmp = $_FILES['fileToUpload']['tmp_name']; $file_type = $_FILES['fileToUpload']['type']; $tmp = explode('.', $_FILES['fileToUpload']['name']); $file_ext = strtolower (end ($tmp)); $extensions = array("jpeg", "jpg", "png", "gif"); if(in_array($file_ext, $extensions) === false) { $errors[] = "extension not allowed. Please choose a JPEG or PNG file."; } if ($file_size > 8097152) { $errors[] = 'File size must be 2 MB'; } if ($width > 1500 || $height > 1500) { echo"File is to large"; } if(!$errors) { $userPic = md5($_FILES["fileToUpload"]["name"]) . $date_time . " " . $file_name; move_uploaded_file($file_tmp, "assets/images/profile_pics/" . $userPic); $stmt = $con->prepare("UPDATE users SET profile_pic = ? WHERE username = ?"); $stmt->bind_param('ss', $userPic, $username); $stmt->execute(); $stmt->close(); } }}else { echo "Invalid Username";}
2 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
您可以分配另一个包含所使用图像的路径和变量的变量,然后在查询中使用该变量:
$file_path = "assets/images/profile_pics/".$userPic;
catspeake
TA贡献1111条经验 获得超0个赞
你的代码:
if(!$errors) {
$userPic = md5($_FILES["fileToUpload"]["name"]) . $date_time . " " . $file_name;
move_uploaded_file($file_tmp,"assets/images/profile_pics/" . $userPic);
$imag_path = "assets/images/profile_pics/" . $userPic;
$stmt = $con->prepare("UPDATE users SET profile_pic = ? WHERE username = ?");
$stmt->bind_param('ss', $imag_path, $username);
$stmt->execute();
$stmt->close();
}
尝试这个:
您仅保存新图像名称,而不保存路径。
- 2 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消