尝试创建照片上传器,但是我不断收到“ user_image”的这些错误,我缺少什么?任何帮助将不胜感激,谢谢:)
错误是:未定义的索引:user_image
PHP代码:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$image_path = mysqli_real_escape_string($conn, 'img/'.$_FILES['user_image']['name']);
//Make sure file is an image
if (preg_match("!image!", $_FILES['user_image']['type'])) {
//copy image to images folder
if (copy($_FILES['user_image']['tmp_name'], $image_path)) {
$_SESSION['user_image'] = $image_path;
$sql = "INSERT INTO users (user_image)";
"VALUES('$image_path')";
//If the query is successful
if ($sql == true) {
$_SESSION['message'] = "Profile picture uploaded.";
} else {
//Error handling
$_SESSION['message'] = "Image could not be uploaded.";
}
} else {
$_SESSION['message'] = "File upload failed.";
}
}
} else {
$_SESSION['message'] = "Please only uplaod JPEG, PNG or GIF files.";
}
HTML代码:
<form class="form" method="post" enctype="multipart/form-data">
<div class="container">
<span class="user"><img src='<?= $_SESSION['user_image']?>'</span>
</div>
<div class="user_image"><input type="file" name="user_image" accept="img/*" /></div>
<button type="submit" name="submit">UPLOAD</button>
</div>
</form>