php - OO 通过PHP上传个人资料图片

标签 php sql

我使用面向对象的技术开发了一个注册表单,它工作得很好,除非我尝试合并文件上传以便用户可以将图片保存在数据库中。

在我的数据库中,我使用 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/

相关文章:

php - 无法使用SSH2连接

php - mysql中多列求和

php - 构建多关系注释或更新表的最佳方式是什么?

php - 使用AJAX从MySQL中获取在PHP中创建的表,不带参数

sql - 通过 SSMS 2012 连接远程数据库时出现错误 "Incorrect syntax near ENspid" "Incorrect syntax near smoEnabled"

mysql - 检查多个字段中的值

mysql - 在mysql中连接具有多个外键的表

c# - C# 应用程序中的 SQL 过滤器和或语句

php - JavaScript 触发 PHP

sql - 忽略 postgresql rank() 窗口函数中的空值