我有一个问题,我无法将图像从表单上传到数据库。问题是我无法获取图像的路径和图像名称。结果,它不会通过if语句,也不会上传到数据库。任何帮助将不胜感激。
<form action="<?php echo htmlspecialchars('register_parse.php');?>" method='post'>
<div class="avatar"><label>Upload a picture: </label><input type="file" name="avatar" accept="image/*" required></div>
</form>
这是register_parse.php文件:
if(isset($_POST['email']) && isset($_POST['avatar'])){
$avatar_path = $mysqli->real_escape_string('img/'.$FILES['avatar']['name']);
// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());
// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}
else{
if(preg_match("!image!",$_FILES['avatar']['type'])){
if(copy($_FILES['avatar']['tmp_name'], $avatar_path)){
$_SESSION['avatar']=$avatar_path;
$sql = "INSERT INTO users (email, image) VALUES ('".$email."','".$avatar_path."')";
$res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if($mysqli->query($sql) === true){
$_SESSION['message'] = "Registration successful.";
header("location: success.php");
}
}
else{
$_SESSION['message'] = "User could not be added to the database.";
header("location: error.php");
}
}
else{
$_SESSION['message'] = "File upload failed";
header("location: error.php");
}
}
}
}
?>
它总是转到FILE Upload Failed。当我删除最后两个if语句if(preg_match(“!image!”,$ _ FILES ['avatar'] ['type']))和if(copy($ _ FILES ['avatar'] ['tmp_name']时, $ avatar_path)),一切正常,但是在数据库中,我得到图像的路径只是img /,因此它无法获得图像的路径。