我使用面向对象的技术开发了一个注册表单,它工作得很好,除非我尝试合并文件上传以便用户可以将图片保存在数据库中。
在我的数据库中,我使用 BLOB 格式并使用 file_get_contents 检索上传的文件。
我之前有 $profilepic = $_POST['photo'];
,这就是为什么 $_POST['photo']
仍在第二个 if 语句中。
我有点困惑该使用什么格式,因为我没有做过很多面向对象的工作。
提前致谢
<center>
<?php
session_start();
include 'registrationform.php';
include 'connection.php';
if (isset($_POST['regsubmit']))
{
$firstname = $_POST['firstname'];
$firstname = ucfirst($firstname);
$lastname = $_POST['lastname'];
$lastname = ucfirst($lastname);
$user = $_POST['username'];
$user = ucfirst($user);
$pass = $_POST['password'];
$spass = $_POST['secondpassword'];
$profilepic = file_get_contents($_FILES['photo']['tmp_name']);
if($_POST['firstname'] && $_POST['lastname'] && $_POST['photo'] && $_POST['username'] && $_POST['password'] && $_POST['secondpassword'])
{
if ($spass == $pass)
{
$query = "INSERT INTO users (firstname, lastname, photo, username, password) VALUES(?, ?, ?, ?, ?)";
$statement = $connection->prepare($query);
$statement->bind_param('ssbss', $firstname, $lastname, $profilepic, $user, $pass);
if($statement->execute()){
print 'Success!';
}else
{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
}
else
{
print 'The passwords do not match!';
}
}
else
{
print 'Enter all fields please';
}
}
?>
</center>
最佳答案
您可以使用 move_uploaded_file() 将上传的图像移动到文件夹中并将图像名称存储在数据库中。
您可以通过以下方式获取图像名称:
$image_name = $_FILES["photo"]["name"];
要显示图像,您可以检索图像名称。
然后在 html 中您可以显示如下图像:
<img src="YOUR_IMAGE_FOLDER_PATH/".$IMAGE_NAME>
关于php - OO 通过PHP上传个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34591660/